无法在Android 2.0+中获得视频缩略图

Man*_*uel 0 video android bitmap thumbnails

我尝试按照此处所述创建视频缩略图.我也在这里阅读参考资料.

在我的应用程序中,我首先让用户选择一个视频:

startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("video/*"), ACTIVITY_PICKVIDEO);
Run Code Online (Sandbox Code Playgroud)

然后我确定视频ID:

fileID = Integer.parseInt(contentUri.getLastPathSegment());
Run Code Online (Sandbox Code Playgroud)

因此,视频content://media/external/video/media/5的ID为5.

然后我尝试获取缩略图位图:

ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, fileID, MediaStore.Video.Thumbnails.MICRO_KIND, options);
Run Code Online (Sandbox Code Playgroud)

抛出没有异常,但位图的宽度和高度为-1.我不确定所需的ID getThubnail()是否实际上是我上面确定的ID.

有没有人知道如果你有内容Uri如何获取缩略图位图的工作示例?

有趣的是(也许是这样)当我尝试使用MediaStore.Video.Thumbnails.MINI_KIND缩略图大小和尝试时,我会得到null .IllegalArgumentException ("Unsupported kind: 2")FULL_SCREEN_KIND

我正在使用Android 2.1的摩托罗拉里程碑.

编辑: 我也尝试通过查询BaseColumns._ID来获取ID,但结果与Uri相同(在给定的示例中,_ID为5).

Jai*_*oni 10

获取视频ID试试这个

String[] proj = {
    MediaStore.Video.Media._ID,
        MediaStore.Video.Media.DISPLAY_NAME,
    MediaStore.Video.Media.DATA
};
Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                                    proj, MediaStore.Video.Media.DISPLAY_NAME+"=?",new String[] {"name.mp4"}, null);
cursor.moveToFirst()
fileid = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));
Run Code Online (Sandbox Code Playgroud)

获取缩略图:

ContentResolver crThumb = getContentResolver();
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb,fileid, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv2.setImageBitmap(curThumb);
Run Code Online (Sandbox Code Playgroud)

这里iv2是imageview,name.mp4将代表你的文件名