android - 无法在 Android 10 上读取缩略图 (loadThumbnail)

标签 android mediastore

我正在通过使用 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());
}

异常(exception):
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

关于android - 无法在 Android 10 上读取缩略图 (loadThumbnail),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60318750/

相关文章:

java - 代码中的 MultiSelectListPreference

java - MediaRecorder.start() 抛出 IllegalStateException

android - 如何直接从 Android 中的 ListView 打开列出的文件

java - 在 Android Q 之前可以获取带有相册的图像,但在 Android Q 上则不行

android - 高效获取 MediaStore 中音频文件的流派

android - 更改 Espresso 的注入(inject)模块

java - 如何随机显示按钮

java - 触摸屏幕后出现 ViewPostIme 指针 1/0 日志

android - 分区存储 : how to delete multiple audio files via MediaStore?

Android MediaStore Crop 功能,如何使选择箭头始终可见?