其他分享
首页 > 其他分享> > android – 从Uri获取视频缩略图

android – 从Uri获取视频缩略图

作者:互联网

我想从我的画廊中选择一个视频.它工作正常.但现在我想显示一个Bitmap,作为缩略图.我厌倦了这段代码,它不起作用,它总是说:NullPointerException

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

这都在onActivityResult中.

如何从视频Uri中获取位图?

谢谢你的帮助

解决方法:

在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);

编辑

Kotlin版

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)

标签:android-bitmap,android,video,thumbnails
来源: https://codeday.me/bug/20191002/1843590.html