MediaMetadataRetriever

MediaMetadataRetriever

我尝试使用MediaMetadataRetriever类从视频获取缩略图:

  MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource("/mnt/sdcard/test.ts");

            Bitmap thumbnail = retriever.getFrameAtTime();
            ivThumbnail.setImageBitmap(thumbnail);

        } catch (Exception ex) {
            ex.printStackTrace();
        }


但是我得到的所有日志:

E/MediaMetadataRetrieverJNI﹕ getFrameAtTime: videoFrame is a NULL pointer


但是文档(http://developer.android.com/guide/appendix/media-formats.html)表示android 3.0+支持此格式

目前,此代码适用于.mp4文件。有什么建议么?

最佳答案

这可能会有所帮助
获取视频uri,并调用此方法。

public void setVideoThumbnail(Uri uri) {
    String[] projection = { BaseColumns._ID, MediaColumns.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndexOrThrow(BaseColumns._ID);
    int id = cursor.getInt(columnIndex);

    ContentResolver crThumb = getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    Bitmap thumbnail= MediaStore.Video.Thumbnails.getThumbnail(crThumb, id,
            MediaStore.Video.Thumbnails.MICRO_KIND, options);
    ivThumbnail.setScaleType(ScaleType.CENTER_CROP);
    ivThumbnail.setImageBitmap(thumbnail);
}

10-07 19:28