Convert string to file and send this image through email attachment - android

I get an image from a PHP server as a string (this image), and I want to send this image by e-mail as a attachment.
When I try to send e-mail, then the attachment is papers but only plain text is displaying at the receiver end. How do I send the e-mail correctly?
This is my code:
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.setType("image/jpeg");
String img_source = listBuzzInfoBean.get(photo).getBuzzImage();
File downloadedPic = new File(Environment.getExternalStorageDirectory(), img_source + ".jpg");// Art_Nature
Log.d("++++++++++++++", img_source);
Uri myUri = Uri.fromFile(downloadedPic);
picMessageIntent.putExtra(Intent.EXTRA_STREAM, myUri); //screenshotUri
startActivity(Intent.createChooser(picMessageIntent, "Share image using"));

I used the following code to handle this. I have noticed that differences between Gmail/Mail apps and how they handle this intent.
Also this post seems to have a similar problem.
Android: Intent.ACTION_SEND with EXTRA_STREAM doesn't attach any image when choosing Gmail app on htc Hero
public static Intent createEmailWithAttachmentIntent(String[] addresses,
String[] ccAddresses, String subject, String contentType,
String filename, String messageBody) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(Intent.EXTRA_EMAIL, addresses);
i.setType(contentType);
if(filename != null && !filename.equals(""))
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filename)));
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, messageBody);
if (ccAddresses != null && ccAddresses.length > 0)
i.putExtra(Intent.EXTRA_CC, ccAddresses);
return i;
}

Related

Sending image via WhatsApp to specific recipient (Android)

I'm sharing my images via WhatsApp - but I have to choose the recipient.
Here is my code:
public static void shareImage(Context context,String path, String text, String otherAppPackage){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
share.putExtra(android.content.Intent.EXTRA_SUBJECT, getSubject(context));
if (text!=null){
share.putExtra(Intent.EXTRA_TEXT,text);
}
if (path!=null){
share.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(path)));
}
context.startActivity(Intent.createChooser(share, context.getString(R.string.share_via)));
}
I wold like to share with someone directly. Does some of you know how can I do this.
Thanks
You can use Intent.ACTION_SENDTO, but message is not copied to clipboard then:
Uri uri = Uri.parse("smsto:+123456789");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.setPackage("com.whatsapp");
it.putExtra("sms_body", "The SMS text");
it.putExtra("chat",true);
startActivity(it);

how to send image via email in android?

I am developing an application in which i have to send an image via email, I tried with
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "xyz#gmal.com");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "message");
emailIntent.setType("image/png");
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.a));
emailIntent.putExtra(Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);
but its giving me as gmail has stopped unfortunately.How can I send an image via email,
Thanks in advance.
Get the image storage path by using the file,
File img = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/", imagename+".png");
Convert that file path in to Uri
Uri imageuri = Uri.fromFile(img);
Send the image via email using
Intent send_img = new Intent(Intent.ACTION_SEND);
send_img.putExtra(Intent.EXTRA_EMAIL, "xyz#gmal.com");
send_img.putExtra(Intent.EXTRA_SUBJECT, "email_subject");
send_img.putExtra(Intent.EXTRA_STREAM, imageuri);
send_img.putExtra(Intent.EXTRA_TEXT, "message");
send_img.setType("text/plain");
send_img.setType("image/png");
startActivity(Intent.createChooser(send_img, "Send Email..."));

Unable to send email with attachments from my app using intents (Gmail)

I'm trying to send files (.log files) contained in a sdcard's folder using Intent. This is the code:
public void sendMail() {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"name.surname#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Log files");
intent.putExtra(Intent.EXTRA_TEXT, "body");
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
File root = Environment.getExternalStorageDirectory();
File logfolder = new File(root, "log");
for (String file : logfolder.list()){
Uri u = Uri.parse(file);
uris.add(u);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(intent, new String("Send mail...")));
}
}
I choose Gmail from menu. Once Gmail is opend it displays a mail with recipient, subject, text, and attachment files correctly. The mail is sended with no error but i get a status bar notification that says "couldn't show attachment"! In fact recipient receives email correctly but it has no attachment.
I'm not able to figure out what is the problem. Why attachments are not sended? Please help me!!
Ok. i find the solution. Need to replace this:
for (String file : logfolder.list()){
Uri u = Uri.parse(file);
uris.add(u);
}
with this:
for (File file : logfolder.listFiles()){
Uri u = Uri.fromFile(file);
uris.add(u);
}

How to attach a image and a .txt(multiple files) file to gmail through Coding?

I am trying to attach an image file and .txt file but only one file is attaching one is not attaching how to attach both the files.
if i use ACTION_SEND_MULTIPLE then the app gets force close.
HERE IS MY CODE:
public static Intent getSendEmailIntent(Context context, String email,
String subject, String body, String fileName) {
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
// Explicitly only use Gmail to send
emailIntent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
emailIntent.setType("*/*");
// Add the recipients
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
new String[] { email }
);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
// Add the attachment by specifying a reference to our custom
// ContentProvider
// and the specific file of interest
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.txt"));
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.jpg"));
return emailIntent;
}
If I put a .jpg file at last before return it is attaching. But if I put a .txt file at last then it is attaching. I want to attach both files pls help.
You can just use the code below and change file path's to your files.
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/IMG.jpg"))); // Add your image URIs here
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/Report.pdf")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
Read this page : send

Android email send without attachment

I have to following problem:
I want to send an email with an image attached to it.
I wrote this code:
File file = context.getDir("Files", context.MODE_WORLD_WRITEABLE);
File image = new File(file, "image.jpg");
Uri U = Uri.fromFile(image);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpg");
i.putExtra(Intent.EXTRA_STREAM, U);
context.startActivity(Intent.createChooser(i, "Email:"));
The email is sent but there is no attachment.
Does anybody have any idea why the email is sent without the attachment?
EDIT
I have found the answer to my question. Because the image was stored on the internal storage, it did not have enough rights so it could not be sent via email. I've moved my image to externalStorage and now it's working :)
Thanks,
Ark
String smsBody = "Body of the Content";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject of the Mail");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, smsBody);
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile("mnt/sdCard/SampleImageFloder/TestImage.png"));
emailIntent.setType("vnd.android.cursor.dir/email");
activity.startActivity(Intent.createChooser(emailIntent,"Email:"));
Try this one -
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"email"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Test");
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(emailIntent);
Just see my Existing Answer also

Categories

Resources