我正在使用MediaStore.Images.Thumbnails.getThumbnail()
通过查询MediaStore从设备读取缩略图。但是,此功能已在Android 10(API 29)中弃用,并带有指向ContentResolver#loadThumbnail
的指针:https://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails
但是,我无法使它正常工作(在运行API 29的模拟设备中)。我已经将一些JPEG复制到了仿真设备上,该文件最终位于“下载”文件夹中。它们在“照片”应用程序中显示良好。以下代码为我提供了FileNotFoundException。 “没有内容提供商”实际上告诉我什么?
try {
Size thumbSize = new Size(100, 100);
Uri thumbUri = Uri.fromFile(new File(imgPath));
// imgPath: /storage/emulated/0/Download/pexels-photo-323490.jpeg
// thumbUri: file:///storage/emulated/0/Download/pexels-photo-323490.jpeg
Bitmap thumbBitmap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
thumbBitmap = mContext.getContentResolver().loadThumbnail(thumbUri, thumbSize, null);
} else {
thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
imgId, MediaStore.Images.Thumbnails.MINI_KIND, null);
}
iconView.setImageBitmap(thumbBitmap);
} catch (Exception e) {
Log.e("err", e.toString());
}
例外:
java.io.FileNotFoundException: No content provider: file:///storage/emulated/0/Download/pexels-photo-323490.jpeg
请尝试一下,希望它对您有用:
int thumbColumn = cur.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID);
int _thumpId = cur.getInt(thumbColumn);
Uri imageUri_t = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,_thumpId);
GGK