How can I share multiple files via an Intent?

2019-03-08 08:11发布

问题:

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() {

            @Override
            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.setType(type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(path)));

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

回答1:

Yes but you'll need to 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);

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.

UPDATE (to the update): Google recently announced that new apps and app updates would be required to target one of the latest versions of Android for release to the Play Store. That said, targeting API 23 or lower is no longer a valid option if you plan to release the app to the store. You must go the FileProvider route.



回答2:

Here is little improved version improvised by MCeley's solution. This could be used to send the heterogeneous file list (like image, document and video at same time), for instance uploading downloaded documents, images at same time.

public static void shareMultiple(List<File> files, Context context){

    ArrayList<Uri> uris = new ArrayList<>();
    for(File file: files){
        uris.add(Uri.fromFile(file));
    }
    final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.setType("*/*");
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(intent, context.getString(R.string.ids_msg_share)));
}


回答3:

/* 
 manifest file outside the applicationTag write these permissions
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> */

    File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                            //Get a top-level public external storage directory for placing files of a particular type. 
                            // This is where the user will typically place and manage their own files, 
                            // so you should be careful about what you put here to ensure you don't 
                            // erase their files or get in the way of their own organization...
                            // pulled from Standard directory in which to place pictures that are available to the user to the File object

                            String[] listOfPictures = pictures.list();
                            //Returns an array of strings with the file names in the directory represented by this file. The result is null if this file is not a directory.

                            Uri uri=null; 
                            ArrayList<Uri> arrayList = new ArrayList<>();
                            if (listOfPictures!=null) {
                                for (String name : listOfPictures) {
                                    uri = Uri.parse("file://" + pictures.toString() + "/" + name );
                                    arrayList.add(uri);
                                }
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                                intent.putExtra(Intent.EXTRA_STREAM, arrayList);
                                //A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
                                intent.setType("image/*"); //any kind of images can support.
                                chooser = Intent.createChooser(intent, "Send Multiple Images");//choosers title
                                 startActivity(chooser);
                            }