How to get Thumbnail in Android API 29

前端 未结 2 842
滥情空心
滥情空心 2021-01-19 16:38

Trying to get thumbnails from a video. Prior to API 29, this was managed using MediaStore.Images.Thumbnails. Code example

Bitmap bitmapThumbnail = ThumbnailU         


        
相关标签:
2条回答
  • 2021-01-19 16:59
    //You can use this 
    val bitmapThumbnail = ThumbnailUtils.createVideoThumbnail(
                File(filePath),
                Size(120, 120),
                null)
    
    0 讨论(0)
  • 2021-01-19 17:04

    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);
    
    0 讨论(0)
提交回复
热议问题