Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.ACTION_VIEW - Multiple files

I have a requirement to show multiple files (2 or more each of images, music or video, but only one type). Given a series of thumbnails or filenames, the user should be able to select a chekbox and preview the selection. IF the user selects multiple images, I want to be able to show ONLY those images selected. If he selects multiple mp3 files, I want to be able to play ONLY those songs.

If the user selects only one file, that's easy to do:

Intent i = new Intent(Intent.ACTION_VIEW);  

if (someType == IMAGE) {
   i.setDataAndType(Uri.fromFile(imageFile),"image/*");
}
else 
   if (sometype == VIDEO) {
      i.setDataAndType(Uri.fromFile(videoFile),"video/mp4");
   }
   else if (someType == MUSIC) {
       i.setDataAndType(Uri.fromFile(musicFile),"audio/mpeg");
   }  
startActivity(i);

However, if the user selects two or more of one type, how can I architect the intent to use whatever image/audio/video apps the user may have installed? Don't want to write custom players/viewers as I know an existing app can do this.

I am trying to mimic the functionality of a cloud app called "AllShare Play" (https://www.samsung.com/us/2012-allshare-play/) . This app does exactly what I need to do in my app - allows the user to "preview" multiple files and uses just the standard "Gallery" app for images, standard music app for songs, etc. In each case, only the files selected are shown or played using standard apps each Android device is loaded with, so I know it's possible to do without writing a viewer or player.

Anyone have any ideas how to send multiple files via Intent.ACTION_VIEW to an external app?

like image 527
Martin Avatar asked Mar 24 '13 16:03

Martin


1 Answers

There is no android standard for send multible to ACTION_VIEW.

But there is a standard for ACTION_SEND_MULTIPLE where the files are transfered via EXTRA_STREAM.

for a working sendmultible example see Secure-Photo-Viewer: you select multible images from a gallery app and send them to Secure-Photo-Viewer.

If your app is the only sender and receiver of this intent you can use the same mechanism with ACTION_VIEW, too.

To learn more on intent communication you can use the intent-intercept app that allows sending and receiving many types of intents

here is the code i use to send multible images

        Intent sendIntent = new Intent();
        sendIntent.setType("image/*");
        if (selectionCount == 1) {
            Long imageId = mSelectedItems.first();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(EXTRA_STREAM, getUri(imageId));
        } else {
            sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

            ArrayList<Uri> uris = new ArrayList<Uri>();

            for (Long mSelectedItem : mSelectedItems) {
                uris.add(getUri(mSelectedItem));
            }
            sendIntent.putParcelableArrayListExtra(EXTRA_STREAM, uris);
        }
        startActivity(sendIntent);
like image 150
k3b Avatar answered Oct 10 '22 15:10

k3b