Creating thumbnail from video file returns null bitmap

大城市里の小女人 提交于 2019-12-04 04:59:47

You can try MediaMetadataRetriever or FFmpegMediaMetadataRetriever. Here is an example:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(intent.getDataString());
Bitmap b = mmr.getFrameAtTime();
mmr.release();

As I remember, argument filePath from createVideoThumbnail should be a classic filepath, not URI.

...

Uri videoUri = intent.getData();
final String realFilePath = getRealPathFromUri();
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(realFilePath, MediaStore.Video.Thumbnails.MICRO_KIND);
...

public String getRealPathFromURI(final Uri contentURI) {
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
        if ( idx == -1 ) {
            return contentURI.getPath();
        }
        String rvalue =  cursor.getString(idx);
        cursor.close();
        return rvalue;
    }
}

EDIT:

Based on the source code of createVideoThumbnail, I created another implementation:

public static Bitmap createVideoThumbnail(Context context, Uri uri, int kind) {
    Bitmap bitmap = null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
        retriever.setDataSource(context, uri);
        bitmap = retriever.captureFrame();
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
        }
    }
    if (kind == Images.Thumbnails.MICRO_KIND && bitmap != null) {
        bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL,
                ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    }
    return bitmap;
}
Manish Bansal

Use this file "mediaFile" and convert it into URI

      Uri uri=Uri.fromFile(mediaFile);

Then pass that URI in the below method. This is working fine at my side.

Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(uri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);

Hope this will help you.

Amir

I faced this problem and solved it this way:

  1. Create FileUtils Class which find path of file for you (I can't find reference of the class so I created a gist)

    String correctedUri = FileUtils.getPath(mContext, Uri.parse(localUri));
    
  2. Use following code

    Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(correctedUri, MediaStore.Video.Thumbnails.MICRO_KIND);
    

EDITED : See this solution it has better performance and easier.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!