2013-03-22 22 views
22

Đây là mã của tôi, nhưng đây là một giải pháp tệp duy nhất.Tôi làm cách nào để chia sẻ nhiều tệp qua Intent?

Tôi có thể chia sẻ nhiều tệp & video tải lên như tôi thực hiện cho các tệp đơn lẻ bên dưới không?

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

Trả lời

66

Có nhưng bạn cần sử dụng Intent.ACTION_SEND_MULTIPLE thay vì 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); 

Điều này chắc chắn có thể được đơn giản hóa nhưng tôi để lại một số dòng để bạn có thể chia nhỏ từng bước cần thiết.

CẬP NHẬT: Bắt đầu từ API 24, chia sẻ tệp URI sẽ gây ra một FileUriExposedException. Để khắc phục điều này, bạn có thể chuyển đổi compileSdkVersion thành 23 hoặc thấp hơn hoặc bạn có thể sử dụng content URIs with a FileProvider.

CẬP NHẬT (cập nhật): Google recently announced cần có ứng dụng và bản cập nhật ứng dụng mới để nhắm mục tiêu một trong các phiên bản Android mới nhất để phát hành lên Cửa hàng Play. Điều đó nói rằng, nhắm mục tiêu API 23 trở xuống không còn là tùy chọn hợp lệ nếu bạn định phát hành ứng dụng cho cửa hàng. Bạn phải đi lộ trình FileProvider.

+0

không may điều này dường như không làm việc trong khi chia sẻ nhiều hình ảnh trên facebook. Tôi đã có thể làm cho nó hoạt động bằng cách sử dụng giải pháp được mô tả trong bài đăng này: http://stackoverflow.com/questions/25846496/intent-share-action-send-multiple-facebook-not-working – Lisitso

+1

'ACTION_SEND_MULTIPLE' đã thực hiện mẹo:) tq – Prabs

+0

Điều gì về việc gửi các loại tệp khác nhau hình ảnh/* và video/*? – Eftekhari

2

Đây là phiên bản cải tiến nhỏ được ứng dụng bởi giải pháp của MCeley. Điều này có thể được sử dụng để gửi danh sách tập tin không đồng nhất (như hình ảnh, tài liệu và video cùng một lúc), ví dụ tải lên tài liệu đã tải xuống, hình ảnh cùng một lúc.

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); 
          } 
Các vấn đề liên quan