2013-03-22 6 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); 

यह निश्चित रूप से सरलीकृत किया जा सकता है लेकिन मैंने कुछ पंक्तियां छोड़ीं ताकि आप आवश्यक प्रत्येक चरण को तोड़ सकें।

अद्यतन: एपीआई 24 में शुरू करना, फाइल यूआरआई साझा करना फाइलयूरीएक्सोस्ड अपवाद का कारण बन जाएगा। इसका समाधान करने के लिए, आप या तो अपने compileSdkVersion को 23 या उससे कम पर स्विच कर सकते हैं या आप content URIs with a FileProvider का उपयोग कर सकते हैं। Google recently announced कि नए ऐप्स और ऐप अपडेट को Play स्टोर पर जारी करने के लिए एंड्रॉयड के नवीनतम संस्करण को लक्षित करने के लिए आवश्यक होगा:

(अद्यतन करने के लिए) अद्यतन। उस ने कहा, यदि आप ऐप को स्टोर में रिलीज़ करने की योजना बना रहे हैं तो एपीआई 23 या निचला लक्ष्य अब वैध विकल्प नहीं है। आपको FileProvider मार्ग जाना होगा।

+0

दुर्भाग्य से इस फेसबुक पर एक से अधिक चित्र साझा करने के दौरान काम नहीं कर रहा है। मैं इस पोस्ट में वर्णित समाधान का उपयोग करके इसे काम करने में सक्षम था: http://stackoverflow.com/questions/25846496/intent-share-action-send-multiple-facebook-not-working – Lisitso

+1

'ACTION_SEND_MULTIPLE' ने चाल की है:) tq – Prabs

+0

विभिन्न फ़ाइल प्रकार छवि/* और वीडियो/* भेजने के बारे में क्या? – Eftekhari

2

यहां एमसीली के समाधान द्वारा सुधारित कुछ बेहतर संस्करण है। इसका उपयोग विषम फ़ाइल सूची (जैसे छवि, दस्तावेज़ और वीडियो एक ही समय में) भेजने के लिए किया जा सकता है, उदाहरण के लिए डाउनलोड किए गए दस्तावेज़ों को अपलोड करना, छवियों को एक ही समय में अपलोड करना।

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); 
          } 
संबंधित मुद्दे

 संबंधित मुद्दे