I want To share Multiple Picture With Single Capti

2019-07-20 05:42发布

问题:

I want to share multiple picture with single caption which is shown on one image not on all of them. But caption will show on every pic which is shared at one time.

Here is my code

private void pic_with_data() {

    Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setPackage("com.whatsapp");
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUriArray);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Download this App");
    shareIntent.setType("text/plain");

    shareIntent.setType("image/jpeg");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    try {
        startActivity(Intent.createChooser(shareIntent, "Share Image!"));
        startActivity(shareIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
    }
}

回答1:

Use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
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);
    files.add(uri);
}

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

Remember, 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.