Android: Let user pick image or video from Gallery

后端 未结 8 612
死守一世寂寞
死守一世寂寞 2021-01-30 20:44

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

Thanks

相关标签:
8条回答
  • 2021-01-30 21:10

    UPDATE 2021

    FINALLY a solution working for Android 9.

    This piece of code only open image apps, and you can select both images and videos. I tried a bunch of different combinations and this exact code will make it work.

        libraryIntent.setType("video/*, image/*");
        String[] mimetypes = {"image/*", "video/*"};
        libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
    
    0 讨论(0)
  • 2021-01-30 21:18

    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
    }
    
    0 讨论(0)
  • 2021-01-30 21:20
    intent.setType("*/*");
    

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

    0 讨论(0)
  • 2021-01-30 21:21

    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/*");
    
    0 讨论(0)
  • 2021-01-30 21:24

    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/*");
    
    0 讨论(0)
  • 2021-01-30 21:25

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题