My app creates mails with attachments, and uses an intent with Intent.ACTION_SEND to launch a mail app.
It works with all the mail apps I tested with, except for the new Gmail 5.0 (it works with Gmail 4.9), where the mail opens without attachment, showing the error: "Permission denied for the attachment".
There are no useful messages from Gmail on logcat. I only tested Gmail 5.0 on Android KitKat, but on multiple devices.
I create the file for the attachment like this:
String fileName = "file-name_something_like_this";
FileOutputStream output = context.openFileOutput(
fileName, Context.MODE_WORLD_READABLE);
// Write data to output...
output.close();
File fileToSend = new File(context.getFilesDir(), fileName);
I'm aware of the security concerns with MODE_WORLD_READABLE.
I send the intent like this:
public static void compose(
Context context,
String address,
String subject,
String body,
File attachment) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(
Intent.EXTRA_EMAIL, new String[] { address });
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.putExtra(
Intent.EXTRA_STREAM,
Uri.fromFile(attachment));
Intent chooser = Intent.createChooser(
emailIntent,
context.getString(R.string.send_mail_chooser));
context.startActivity(chooser);
}
Is there anything I do wrong when creating the file or sending the intent? Is there a better way to start a mail app with attachment? Alternatively - has someone encountered this problem and found a workaround for it?
Thanks!
I was able to pass a screenshot .jpeg file from my app to GMail 5.0 through an Intent. The key was in this answer.
Everything I have from #natasky 's code is nearly identical but instead, I have the file's directory as
context.getExternalCacheDir();
Which "represents the external storage directory where you should save cache files" (documentation)
GMail 5.0 added some security checks to attachments it receives from an Intent. These are unrelated to unix permissions, so the fact that the file is readable doesn't matter.
When the attachment Uri is a file://, it'll only accept files from external storage, the private directory of gmail itself, or world-readable files from the private data directory of the calling app.
The problem with this security check is that it relies on gmail being able to find the caller app, which is only reliable when the caller has asked for result. In your code above, you do not ask for result and therefore gmail does not know who the caller is, and rejects your file.
Since it worked for you in 4.9 but not in 5.0, you know it's not a unix permission problem, so the reason must be the new checks.
TL;DR answer:
replace startActivity with startActivityForResult.
Or better yet, use a content provider.
Use getExternalCacheDir() with File.createTempFile.
Use the following to create a temporary file in the external cache directory:
File tempFile = File.createTempFile("fileName", ".txt", context.getExternalCacheDir());
Then copy your original file's content to tempFile,
FileWriter fw = new FileWriter(tempFile);
FileReader fr = new FileReader(Data.ERR_BAK_FILE);
int c = fr.read();
while (c != -1) {
fw.write(c);
c = fr.read();
}
fr.close();
fw.flush();
fw.close();
now put your file to intent,
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
You should implement a FileProvider, which can create Uris for your app's internal files. Other apps are granted permission to read these Uris. Then, simply instead of calling Uri.fromFile(attachment), you instantiate your FileProvider and use:
fileProvider.getUriForFile(attachment);
Google have an answer for that issue:
Store the data in your own ContentProvider, making sure that other apps have the correct permission to access your provider. The preferred mechanism for providing access is to use per-URI permissions which are temporary and only grant access to the receiving application. An easy way to create a ContentProvider like this is to use the FileProvider helper class.
Use the system MediaStore. The MediaStore is primarily aimed at video, audio and image MIME types, however beginning with Android 3.0 (API level 11) it can also store non-media types (see MediaStore.Files for more info). Files can be inserted into the MediaStore using scanFile() after which a content:// style Uri suitable for sharing is passed to the provided onScanCompleted() callback. Note that once added to the system MediaStore the content is accessible to any app on the device.
Also you can try set permissions for your file:
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
And finally you can copy/store your files in external storage - permissions not needed there.
I tested it and I found out that it was definitely private storage access problem.
When you attach some file to Gmail (over 5.0) do not use the file from private storage such as /data/data/package/. Try to use /storage/sdcard.
You can successfully attach your file.
Not sure why GMail 5.0 doesn't like certain file paths (which I've confirmed it does have read access to), but an apparently better solution is to implement your own ContentProvider class to serve the file. It's actually somewhat simple, and I found a decent example here: http://stephendnicholas.com/archives/974
Be sure to add the tag to your app manifest, and include a "android:grantUriPermissions="true"" within that. You'll also want to implement getType() and return the appropriate MIME type for the file URI, otherwise some apps wont work with this... There's an example of that in the comment section on the link.
I was having this problem and finally found an easy way to send email with attachment. Here is the code
public void SendEmail(){
try {
//saving image
String randomNameOfPic = Calendar.DAY_OF_YEAR+DateFormat.getTimeInstance().toString();
File file = new File(ActivityRecharge.this.getCacheDir(), "slip"+ randomNameOfPic+ ".jpg");
FileOutputStream fOut = new FileOutputStream(file);
myPic.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
//sending email
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"zohabali5#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Recharge Account");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
//Uri uri = Uri.parse("file://" + fileAbsolutePath);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent, "Send email..."),12);
}catch (Exception e){
Toast.makeText(ActivityRecharge.this,"Unable to open Email intent",Toast.LENGTH_LONG).show();
}
}
In this code "myPic" is bitmap which was returned by camera intent
Step 1: Add authority in your attached URI
Uri uri = FileProvider.getUriForFile(context, ""com.yourpackage", file);
Same as your manifest file provide name
android:authorities="com.yourpackage"
Step 2`; Add flag for allow to read
myIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
I have an app. It's not an email app. But it has a feature similar to email, where a user may select to send an attachment along with a message. When I click to add attachment on my gmail, for example, I get the option to attach anything such as video, music, picture, file, etc. Has anyone ever build such an intent and unmarshalled the data and don't mind sharing their expertise?
edit
I am not able to get the multiple mime types in. I need the intent to be able to get video or image or music:
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_PICK);
attachment.setType("image/*,video/*,audio/*");
startActivityForResult(attachment, ATTACHMENT_REQUEST_CODE);
}
Right now it only launches the Photos or Gallery app. It does not load for audio, whereas the Gmail app attachment button does.
I think I got it, but I can't say 100% yet so I will reward #CommonsWare after testing
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_GET_CONTENT);
attachment.setType("*/*");
startActivityForResult(Intent.createChooser(attachment, "Pick Attachment"),
ATTACHMENT_REQUEST_CODE);
}
If you are trying to get content based on MIME type, use an ACTION_GET_CONTENT Intent with startActivityForResult(). The Uri delivered to your onActivityResult() method will point to the piece of content that the user chose.
I think you should simulate a html form(by assembling http header, parameters), and post your attachment to your server likes picking a file then submit the form
When I am Using this code to send mms to specific user it shows me popup to send it via gmail,whatsapp,gtalk,message and etc. But in my case I just want to send that image as an mms to specific number that i will define in address field whithout showing any popup can any body tell me How to do this ? I googled for this and find lot of stuff on it.
Here is my code*strong text*
public void sendData(int num){
String fileString = "..."; //put the location of the file here
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.putExtra("sms_body", "text");
mmsIntent.putExtra("address", num);
mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fileString)));
msIntent.setType("image/jpeg");
startActivity(Intent.createChooser(mmsIntent, "Send"));
}
Using an intent (like you did) is the preferred way because it's easy to implement and let the user choose his favorite app for the task of sending the MMS.
That being said you can still implement yourself the operation and send the MMS programmatically from your app by crafting and sending the appropriate HTTP request.
The following answer will provide you all the information you need: How to send image via MMS in Android?
I'm developing a very small application for Android 2.3.3.
I want to send an email (through the android email app) containing a jpeg image as an attachment, below the relevat code (tested only with sdk emulator):
public void sendArtwork(View aView){
EditText subj = (EditText)findViewById(R.id.edit_subj);
EditText descr = (EditText)findViewById(R.id.edit_descr);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/jpeg"); // attachment is a jpeg
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{"contribute#unintentional.org"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,subj.getText().toString()); //get subject from one EditText in the UI
emailIntent.putExtra(Intent.EXTRA_TEXT,descr.getText().toString()); //get body from one EditText in the UI
emailIntent.putExtra(Intent.EXTRA_STREAM, fileURI); // add attachment
startActivityForResult(Intent.createChooser(emailIntent, "Choose Email application:"), EMAIL_CODE);
}
It works as expected: it opens a Chooser, creates an email with the correct address, subject, text and attachment and sends it.
The only thing I'm not able to accomplish is to set the correct mime type for the image: the attachment is received correctly (i can detach it to disk and open it) but without a content type, so the email client (Thunderbird) does not display a preview and is not able to provide an application to open it.
Does anybody have advice on this?
----EDIT
The image file is sent across without any errors: as said, if I save it on disk on my PC and open it using a suitable application (i.e. Picasa) it shows up correctly.
I'm using the same method for sending emails, and have tested on various versions of a few email clients.
Even gmail is inconsitent, some versions sets the mime type of the attachment, others ignore it.
I've came to the conclusion that there is no safe solution. At least not by using an ACTION_SEND Intent.
My application allows users to create and modify files. I would like them to be able to send a file as an email attachment. So, I need to first create and write to a temporary file, which I then attach to the email. And then I would like to delete the temporary file when the email program finishes. Unfortunately, the gmail app responds with a result code as soon as the user clicks "send"; and if I delete the file as soon as the result code is received, no attachment is sent.
Its possible that something else is going wrong and the attachment is not sent for a different reason, but I'm pretty sure my assessment is correct because the below code works properly if I comment out the mEmailTmpFile.delete() call. It also works fine if I do something very undesirable like Thread.sleep(4000) immediately prior to mEmailTmpFile.delete().
Is there anyway to be notified when the email is done sending? Or any other suggestions for how I should work around this?
//send an email...
File externalStorage = Environment.getExternalStorageDirectory();
String sdcardPath = externalStorage.getAbsolutePath();
mEmailTmpFile = new File(sdcardPath + "/" + name );
//do some other to ensure unqiueness and then write to the file...
//all done writing, send email
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/zip");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, name);
sendIntent.putExtra(Intent.EXTRA_TEXT, "File attached.");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ mEmailTmpFile.getPath()));
startActivityForResult(Intent.createChooser(sendIntent, "Email"), REQUESTCODE_EMAIL);
public synchronized void onActivityResult(int reqCode, int resultCode, Intent data)
{
if (reqCode == REQUESTCODE_EMAIL)
{
mEmailTmpFile.delete();
}
}
In my apps, I don't delete the temporary file. Android will take care of it by deleting the file if it needs space. I would ensure that you don't create the tmp file in the SDCard root directory since that can look messy but other than that there shouldn't be a problem.