osmund sadler osmund sadler - 1 year ago 105
Android Question

How can I share multiple files via an Intent?

Here is my code, but this is for a single file solution.

Can I share multiple files & uploads like I do for single files below?

Button btn = (Button)findViewById(R.id.hello);

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/pic.png";
File file = new File(path);

MimeTypeMap type = MimeTypeMap.getSingleton();

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.putExtra(Intent.EXTRA_TEXT, "1111");

Answer Source

Yes but you'll need to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND.

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg"); /* This example is sharing jpeg images. */

ArrayList<Uri> files = new ArrayList<Uri>();

for(String path : filesToSend /* List of the files you want to send */) {
    File file = new File(path);
    Uri uri = Uri.fromFile(file);

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);

This could definitely be simplified but I left some lines in so you can break down each step that is needed.

UPDATE: Starting in API 24, sharing file URIs will cause a FileUriExposedException. To remedy this, you can either switch your compileSdkVersion to 23 or lower or you can use content URIs with a FileProvider.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download