简体   繁体   中英

MediaStorage.Images.Thumbnails is deprecated in API29. How to use the loadThumbnail()?

Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr,id,MediaStore.Images.Thumbnails.MINI_KIND);
MediaMetaDataRetriever mmr = new MediaMetaDataRetriever();
mmr.setDataSource("path of your file");  //===========Other Options also available
byte[] data = mmr.getEmbededPicture();
if(data!=null){
    //========Create Bitmap with byte array======//
}else{
    //========Do your code if media has no image====//
}

You can use it by checking the SDK version first. If you queried for media, the mediaUri will be constructed by appending the media ID to the content URI, eg mediaUri = ContentUris.withAppendedId(imagesContentUri, id)

val thumbnail = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val cs = CancellationSignal()
    context.contentResolver.loadThumbnail(mediaUri, Size(100, 100), cs)
} else {
    MediaStore.Images.Thumbnails.getThumbnail(context.contentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM