How to send zip file as attachment in email client

Hi Guys,

I tried following code to attach a zip file

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(STORAGE_DIRECTORY_PATH));
sendIntent.setType("text/html");
startActivity(sendIntent);

in the above code

STORAGE_DIRECTORY_PATH=Environment.getExternalStorageDirectory().getPath() + "/MyProj/test.xml";

is a String variable used for file path.

I see a toast message saying this

Read access denied. The selected file cannot be read

I see the fix for it is replace Uri.parse with Uri.fromFile as per this link

File zipfile=new File(STORAGE_ZIP_FILE);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
 sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(zipfile));
 sendIntent.setType("text/html");
 startActivity(sendIntent);

It solved the issue

as you see i used text/html  mime type as setType, but here I see we can also use “multipart/x-zip” Which seems reasonable and it’s too works fine.

Hope it helps some body…. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *