Android: Let user pick image or video from Gallery

后端 未结 8 624
死守一世寂寞
死守一世寂寞 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: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
    }
    

提交回复
热议问题