Android: Let user pick image or video from Gallery

2019-02-01 05:24发布

问题:

Is it possible to to start Gallery in such a way so both pictures and videos are shown?

Thanks

回答1:

Pick Audio file from Gallery:

//Use MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);

Pick Video file from Gallery:

//Use MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);

Pick Image from gallery:

//Use  MediaStore.Images.Media.EXTERNAL_CONTENT_URI
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

Pick Media Files or images:

 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/* video/*");


回答2:

You start the gallery as such:

Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/* video/*");
startActivityForResult(pickIntent, IMAGE_PICKER_SELECT);

then in your onActivityResult you can check if video or image was selected by doing this:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {
    Uri selectedMediaUri = data.getData();
    if (selectedMediaUri.toString().contains("image")) {
        //handle image
    } else  if (selectedMediaUri.toString().contains("video")) {
        //handle video
    }
}


回答3:

(EDIT: I don't use it anymore, we went back to the two choices "pick image" and "pick video". The problem was with some Sony phone. So, it's not 100% solution below, be careful! )

This is what I use:

if (Build.VERSION.SDK_INT < 19) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/* video/*");
    startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)), SELECT_GALLERY);
} else {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
    startActivityForResult(intent, SELECT_GALLERY_KITKAT);
}

The key here is intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});



回答4:

intent.setType("*/*");

This presents user with dialog but works on at least ICS. Haven't tested on other platforms.



回答5:

When you need to determine what kind of content was returned, you can do it using content resolver to get the MIME type of the returned content:

if( data != null) {
    Uri selectedUri = data.getData();   
    String[] columns = { MediaStore.Images.Media.DATA,
                         MediaStore.Images.Media.MIME_TYPE };

    Cursor cursor = getContentResolver().query(selectedUri, columns, null, null, null);
    cursor.moveToFirst();

    int pathColumnIndex     = cursor.getColumnIndex( columns[0] );
    int mimeTypeColumnIndex = cursor.getColumnIndex( columns[1] );

    String contentPath = cursor.getString(pathColumnIndex);
    String mimeType    = cursor.getString(mimeTypeColumnIndex);
    cursor.close();

    if(mimeType.startsWith("image")) {
          //It's an image
    }
    else if(mimeType.startsWith("video")) {
         //It's a video
    }       
}
else {
    // show error or do nothing
}


回答6:

CoolIris which came with my galaxy tab can do it. However the cooliris on my acer betouch will not :S On my milestone you can not start the gallery with a pick intent on the video url however when you start it on the images url, you can select a video and it will return a video url too.



回答7:

You need use the following as picking Intent

Intent photoLibraryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoLibraryIntent.setType("image/* video/*");


回答8:

No, it's not possible with the stock Gallery app. You can try and search for an app that does this.