Cassie Cassie - 5 months ago 16
Android Question

How to share folders via intent?

I have found this code for sharing image files via bluetooth/cloud storage/wifi. but how do i share the whole folder instead? here is my code-

private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);

// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");

// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";

File imageFileToShare = new File(imagePath);

Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(share, "Share Image!"));
}

Answer
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("*/*"); /* allow any file type */

//Get all files in this particular location
File[] filesToSend = new File("/sdcard/myDocs").listFiles();

ArrayList<Uri> files = new ArrayList<Uri>();
for (File file : filesToSend) {
    Uri uri = Uri.fromFile(file);
    files.add(uri);
}

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);