Trying to attach a file from SD Card to email Trying to attach a file from SD Card to email android android

Trying to attach a file from SD Card to email


Also getting the same problem

Code:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);     emailIntent.setType("image/jpeg");    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]     {"me@gmail.com"});     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,     "Test Subject");     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,     "go on read the emails");     Log.v(getClass().getSimpleName(), "sPhotoUri=" + Uri.parse("file:/"+ sPhotoFileName));    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ sPhotoFileName));    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

From adb logcat:

V/DumbDumpersMain( 3972):   sPhotoUri=file://sdcard/DumbDumpers/DumbDumper.jpgI/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.CHOOSER comp={android/com.android.internal.app.ChooserActivity} (has extras) }I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x3000000 comp={com.google.android.gm/com.google.android.gm.ComposeActivityGmail} (has extras) }I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x2800000 comp={com.google.android.gm/com.google.android.gm.ComposeActivity} (has extras) }D/gmail-ls(  120):      MailProvider.query: content://gmail-ls/labels/me@gmail.com(null, null)D/Gmail   ( 2507):      URI FOUND:file://sdcard/DumbDumpers/DumbDumper.jpg

Looks like the email provider is attaching a 0 length file. When I check the filesystem the file is there and correct. The code which creates the image file is well finished prior to the attempt to email it.

Anyone fixed this without magic reboots (I've already tried that)?

Regards,
Fin

Update

Path for me should have been

file:///sdcard/DumbDumpers/DumbDumper.jpg

you need the extra / as this points to the root directory, i.e.:

file:// + /sdcard/DumbDumpers/DumbDumper.jpg

combined as

file:///sdcard/DumbDumpers/DumbDumper.jpg

In the above snippet you need:

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ sPhotoFileName));

I hope this helps. It took me ages to debug.

Regards,
Finlay


Just a little remark from my side. I've been having the same issues with GMail, but somehow it seems to work when I store the file in question on the SD card first and retrieve it from there, rather than from the assets. So my code is the following:

Intent i = new Intent(Intent.ACTION_SEND);i.putExtra(Intent.EXTRA_SUBJECT, "Title");i.putExtra(Intent.EXTRA_TEXT, "Content");i.putExtra(Intent.EXTRA_STREAM, uri);i.setType("text/plain");startActivity(Intent.createChooser(i, "Send mail"));

and here,

uri = Uri.fromFile(new File(context.getFilesDir(), FILENAME));

does not work, whereas

uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));

does.

Regards,Michael


instead of "Uri.parse" use "Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"file name"))"

Environment.getExternalStorageDirectory() - path to SDcard or any other external storage