如何在Android API 29中获取缩略图 [英] How to get Thumbnail in Android API 29
问题描述
尝试从视频中获取缩略图.在API 29之前,这是使用MediaStore.Images.Thumbnails进行管理的.代码示例
Trying to get thumbnails from a video. Prior to API 29, this was managed using MediaStore.Images.Thumbnails. Code example
Bitmap bitmapThumbnail = ThumbnailUtils.createVideoThumbnail(videoArrayList.get(position).getPath(), MediaStore.Images.Thumbnails.MINI_KIND);
但是在API 29中,MediaStore.Images.Thumbnails被声明为已弃用.Google提供了ContentResolver#loadThumbnail.告诉我如何使用它.
But in API 29 MediaStore.Images.Thumbnails is declared deprecated. Google offers ContentResolver # loadThumbnail. Tell me how to use it.
推荐答案
createVideoThumbnail
从以下位置更改:
公共静态位图createVideoThumbnail(字符串filePath,int类型)
public static Bitmap createVideoThumbnail (String filePath, int kind)
到
公共静态位图createVideoThumbnail(文件文件,大小大小,CancellationSignal信号)
public static Bitmap createVideoThumbnail (File file, Size size, CancellationSignal signal)
因此,您在问题中提供的示例将更改为以下内容:
So, the example you have provided in your question, will change to the following:
Size mSize = new Size(96,96);
CancellationSignal ca = new CancellationSignal();
Bitmap bitmapThumbnail = ThumbnailUtils.createVideoThumbnail(new File(videoArrayList.get(position).getPath()), mSize, ca);
您可以使用 CancellationSignal
通过调用 ca.cancel();
取消创建 Bitmap
.
You can use CancellationSignal
to cancel the creation of the Bitmap
by calling ca.cancel();
.
Size
的值取决于所需的 Bitmap
的大小:
The Size
value depends on the size of Bitmap
you want:
大小:屏幕上将显示此缩略图的目标区域.这将以EXTRA_SIZE的形式传递给提供程序,以帮助其避免下载或生成大量资源.此值不能为空.
Size: The target area on the screen where this thumbnail will be shown. This is passed to the provider as EXTRA_SIZE to help it avoid downloading or generating heavy resources. This value cannot be null.
您还可以使用 loadThumbnail
,它接受 Uri
而不是 File
对象,例如:
You can also use loadThumbnail
, which accepts a Uri
instead of a File
object, like this:
公共位图loadThumbnail(Uri uri,大小大小,CancellationSignal信号)
public Bitmap loadThumbnail (Uri uri, Size size, CancellationSignal signal)
因此您的代码将如下所示:
So your code will then look like this:
Uri mUri = ...; // Your Uri
Size mSize = new Size(96,96);
CancellationSignal ca = new CancellationSignal();
Bitmap bitmapThumbnail = getContentResolver().loadThumbnail(mUri, mSize, ca);
这篇关于如何在Android API 29中获取缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!