Get Video Thumbnail from Uri

前端 未结 7 1229
遇见更好的自我
遇见更好的自我 2020-12-19 07:45

I want to select a video from my Gallery. It\'s working fine. But now I want to display a Bitmap, as a thumbnail.I tired this code it\'s not working, it always says: NullPoi

相关标签:
7条回答
  • 2020-12-19 08:18

    in onActivityResult

    String[] filePathColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();
    
    Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(picturePath, MediaStore.Video.Thumbnails.MICRO_KIND);
    

    Edit

    Kotlin version

    val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
    val cursor = context.contentResolver.query(uri, filePathColumn, null, null, null)
    cursor.moveToFirst()
    
    val columnIndex = cursor.getColumnIndex(filePathColumn[0])
    val picturePath = cursor.getString(columnIndex)
    cursor.close()
    
    val bitmap = ThumbnailUtils.createVideoThumbnail(picturePath, MediaStore.Video.Thumbnails.MICRO_KIND)
    
    0 讨论(0)
  • 2020-12-19 08:27
    1. createVideoThumbnail() needs the file path, not the content uri.
    2. The file path requires external read permissions.

    If you're getting null responses, it may be from using the content uri (though the assumption in ThumbnailsUtils.java is of a corrupt video file). When I fixed that, then I was getting permissions errors.

    I was able to get the file path from the content uri using the video's ID like this:

    val selection = MediaStore.Video.Media._ID + " = $id"
    val cursor = 
    this.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, 
    selection, null, null)      
    

    And then continue on with the cursor as in the other answers in SO.

    Docs for contentResolver.query()

    0 讨论(0)
  • 2020-12-19 08:28

    This works for me:

    Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);
    

    Using ThumbnailUtils, you can create thumbnail of two types.

    MediaStore.Images.Thumbnails.MICRO_KIND - type will generate thumbnail of size 96 x 96. MediaStore.Images.Thumbnails.MINI_KIND - type will generate thumbnail of size 512 x 384.

    0 讨论(0)
  • 2020-12-19 08:31

    In the latest API 24, you may face some issues if you stick with an approach in the accepted answer.

    for example in this line int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); sometimes I got W/System.err: java.lang.IllegalArgumentException: column '_data' does not exist error message.

    also in the latest API, you may get SecurityException if you deal with widgets or shared content. Keep that in mind.

    As for the video thumbnail from Uri - I use an approach which utilizes MediaMetadataRetriever, thus you don't need to get String filePath:

                MediaMetadataRetriever mMMR = new MediaMetadataRetriever();
                mMMR.setDataSource(context, videoUri);
                bmp = mMMR.getFrameAtTime();
    

    Hope this helps

    0 讨论(0)
  • 2020-12-19 08:31

    Try this one:

    Bitmap bitmap2 = ThumbnailUtils.createVideoThumbnail( uri.getPath() , MediaStore.Images.Thumbnails.MINI_KIND );
    
    0 讨论(0)
  • 2020-12-19 08:39

    For API 27, for document URI (1000 is microseconds)

    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource( context, doc_uri );
    Bitmap bm = mmr.getScaledFrameAtTime( 1000, MediaMetadataRetriever.OPTION_NEXT_SYNC, 128, 128 );
    
    0 讨论(0)
提交回复
热议问题