Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Thumbnail in Android API 29

Tags:

android

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

But in API 29 MediaStore.Images.Thumbnails is declared deprecated. Google offers ContentResolver # loadThumbnail. Tell me how to use it.

like image 982
Александр Артёмов Avatar asked Dec 04 '19 20:12

Александр Артёмов


2 Answers

createVideoThumbnail was changed from:

public static Bitmap createVideoThumbnail (String filePath, int kind)

to

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

You can use CancellationSignal to cancel the creation of the Bitmap by calling ca.cancel();.

The Size value depends on the size of Bitmap you want:

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.


You can also use loadThumbnail, which accepts a Uri instead of a File object, like this:

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);
like image 87
ClassA Avatar answered Oct 30 '22 01:10

ClassA


//You can use this 
val bitmapThumbnail = ThumbnailUtils.createVideoThumbnail(
            File(filePath),
            Size(120, 120),
            null)
like image 41
Vinod Kamble Avatar answered Oct 30 '22 01:10

Vinod Kamble