2013-03-22 13 views
22

Ecco il mio codice, ma questo è per una soluzione a singolo file.Come posso condividere più file tramite un intent?

Posso condividere più file & caricamenti come faccio per i singoli file di seguito?

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

risposta

66

Sì, ma avrete bisogno di utilizzare Intent.ACTION_SEND_MULTIPLE invece di 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); 

Questo potrebbe sicuramente essere semplificata, ma ho lasciato alcune linee in modo da poter abbattere ogni passo che è necessario.

UPDATE: a partire dall'API 24, la condivisione degli URI dei file causerà un'eccezione FileUriExposedException. Per rimediare a questo, è possibile passare a compileSdkVersion a 23 o inferiore oppure è possibile utilizzare content URIs with a FileProvider.

UPDATE (per l'aggiornamento): Google recently announced che le nuove applicazioni e aggiornamenti app sarebbero tenuti a bersaglio una delle ultime versioni di Android per il rilascio nel Play Store. Detto questo, l'API di destinazione 23 o inferiore non è più un'opzione valida se si prevede di rilasciare l'app nello store. Devi seguire il percorso FileProvider.

+0

purtroppo questo sembra che non funziona, mentre la condivisione foto multiple su facebook. Sono stato in grado di farlo funzionare utilizzando la soluzione descritta in questo post: http://stackoverflow.com/questions/25846496/intent-share-action-send-multiple-facebook-not-working – Lisitso

+1

'ACTION_SEND_MULTIPLE' ha fatto il trucco:) tq – Prabs

+0

Per quanto riguarda l'invio di diversi tipi di file image/* e video/*? – Eftekhari

2

Ecco una versione migliorata improvvisata dalla soluzione di MCeley. Questo potrebbe essere usato per inviare la lista di file eterogenei (come immagini, documenti e video allo stesso tempo), ad esempio caricando i documenti scaricati, le immagini allo stesso tempo.

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