【问题标题】:Can't read thumbnails on Android 10 (loadThumbnail)无法在 Android 10 上读取缩略图 (loadThumbnail)
【发布时间】:2020-02-20 11:18:35
【问题描述】:

我正在通过使用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

【问题讨论】:

  • 您是否创建了内容提供者? developer.android.com/guide/topics/providers/…
  • 是的,我有一个与 MediaStore 接口的 ContentProvider:public class PhotoProvider extends ContentProvider。我的查询方法返回一个光标,其中包含缩略图“数据”(即路径)等字段:String thumbData = cursor.getString(PhotoFragment.COL_THUMB_DATA);
  • 粘贴的代码来自我的RecyclerViewCursorAdapter 类,它将光标中的一行绑定到 RecyclerView(网格)中的视图元素。
  • file:///storage/emulated/0/Download/pexels-photo-323490.jpeg 如果您使用这样的 uri,则不涉及内容提供者。也不是你的。函数 .loadThumbnail() 永远不会使用这样的 uri。
  • 请试试这个,希望对你有用int thumbColumn = cur.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID); int _thumpId = cur.getInt(thumbColumn); Uri imageUri_t = imageUri_t = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,_thumpId);

标签: android mediastore


【解决方案1】:

请试试这个,希望它对你有用:

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

【讨论】:

  • 谢谢,@GGK!我现在得到一个可用的 uri,类型为 content://media/external/images/media/31
  • 为什么我没有使用 loadThumbnail 函数使用 MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL) 的查询 Uri 获取 mp3 文件的专辑封面的这个 Uri?它一直让我FileNotFoundException(并且我已授予存储权限)...您能否为此发布完整的示例代码?或者这可能只适用于图像文件?
  • 那是专辑艺术嵌入在 mp3 音频文件中。您必须使用uri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL); 而不是使用MediaMetadataRetriever 获取专辑封面
【解决方案2】:

获取缩略图和所有 Android 版本的最佳答案是:

val thumbnail: Bitmap = if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)) {
    mContentResolver.loadThumbnail(contentUri, Size(width / 2, height / 2), null)
} else {
    MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null)
}

【讨论】:

    【解决方案3】:

    uri 可能不太好。我的意思是尝试使用 FileProvider 来获取文件的 uri。如果您的 legecyExternalStorage 为 false,那么您将无法访问这样的文件。 android 12 也是如此。您需要使用 MediaStore 来获取 contentUri

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      相关资源
      最近更新 更多