Android sharing Files, by sending them via email or other apps

user2351234 picture user2351234 · Aug 1, 2013 · Viewed 57.6k times · Source

I have a list of files in my android app and I want to be able to get the selected items and send them via email or any other sharing app. Here is my code.

Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds());
                    sendIntent.setType("text/plain");
                    startActivity(sendIntent);

Answer

Tushar Mate picture Tushar Mate · Feb 24, 2015

this is the code for sharing file in android

Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(myFilePath);

if(fileWithinMyDir.exists()) {
    intentShareFile.setType("application/pdf");
    intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myFilePath));

    intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
                        "Sharing File...");
    intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");

    startActivity(Intent.createChooser(intentShareFile, "Share File"));
}