Skip to content Skip to sidebar Skip to footer

Attach A Local File To An Email

I can't attach an image file located in my cache to an email Intent. My code : Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setType('plain/text'); emailIntent.p

Solution 1:

You cannot simply pass the file path of a file that lies in your app's folder. Files in that folder can only be accessed by your app.

You'll have to implement a ContentProvider for allowing external apps to access your files.

This will get you started: http://developer.android.com/guide/topics/providers/content-provider-basics.html

For files, you can also use FileProvider: http://developer.android.com/reference/android/support/v4/content/FileProvider.html

Solution 2:

Data from your app can not be accessed by other apps. A quick solution is to copy the file to the external folder and once you get the result back from the email activity, delete the external file.

StringoldPath= ...;
StringnewPath= getExternalCacheDir() + "/" + UUID.randomUUID();

// Copy the file from old path to new path
...

Uriuri= Uri.fromFile(image_file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);                                                                                                                 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello world");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

publicvoidonActivityResult(int requestCode, int resultCode, final Intent data) {
    // Delete the newPath file
}

Solution 3:

After many intents, here is the answer that worked for me : Android attach image to email doesn't work

You just need to copy the image to the external storage and then attach this new image.

Solution 4:

I know it is deprecated but still works as of v4.4.4. Just put your file into context.getFilesDir() and specify world readable flag

FileOutputStream fos = context.openFileOutput(LOG_FILE_NAME, Context.MODE_WORLD_READABLE);
fos.write(...);
fos.close();

Then use this location to attach the file

Stringattachment= context.getFileStreamPath(LOG_FILE_NAME).getAbsolutePath();
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attachment));

Post a Comment for "Attach A Local File To An Email"