2013-03-22 44 views
22

這是我的代碼,但這是針對單個文件的解決方案。如何通過意圖共享多個文件?

我可以共享多個文件嗎&像我爲單個文件一樣上傳?

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); 
      } 
     }); 

回答

66

是的,但你需要使用Intent.ACTION_SEND_MULTIPLE,而不是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); 

這絕對可以簡化,但我留下了一些行,所以你可以分解每一個需要的步驟。

UPDATE:從API 24開始,共享文件URI將導致FileUriExposedException。爲了解決這個問題,你可以將你的compileSdkVersion切換到23或更低,或者你可以使用content URIs with a FileProvider

更新(更新)Google recently announced需要新的應用程序和應用程序更新才能將最新版本的Android設備發佈到Play商店。也就是說,如果您打算將應用程序發佈到商店,則針對API 23或更低版本不再是有效的選項。你必須去FileProvider路線。

+0

不幸的是,這似乎同時在Facebook上分享多張圖片不能正常工作。我能夠使用本文中描述的解決方案使其工作:http://stackoverflow.com/questions/25846496/intent-share-action-send-multiple-facebook-not-working – Lisitso 2015-01-16 10:41:40

+1

ACTION_SEND_MULTIPLE'做了竅門: )tq – Prabs 2015-09-08 06:44:40

+0

發送不同的文件類型image/*和video/*怎麼辦? – Eftekhari 2016-07-05 20:07:44

2

這裏是由MCeley的解決方案即興創作的小改進版本。這可以用來發送異構文件列表(例如圖像,文檔和視頻),例如同時上傳下載的文檔,圖像。

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))); 
} 
1
/* 
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); 
          }