Android

How to send zip file as attach in email client

Hi Guys, When I’m trying to attach zip file to a mail composer. I tried following code

//this is a String variable used for file path.
String STORAGE_ZIP_FILE=Environment.getExternalStorageDirectory().getPath() + "/MyProj/test.zip";

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

But, I see runtime error as it displayed a toast message saying this

Read access denied. The selected file cannot be read.

Solution:

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 as per this link here. I see we can also use “multipart/x-zip” which seems meaningful and it’s works fine too.

Hope it helps somebody….

Cheers

Leave a Reply

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