android - 无法在 Android 10 上更新 MediaStore

标签 android mediastore android-10.0

我一直在通过 ContentResolver 更新 MediaStore 中的元数据,但这不再适用于 Android Q (API 29)。以下代码给了我一个警告,并且描述没有更新:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DESCRIPTION, "Some text");

int res = getContext().getContentResolver().update(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        values,
        MediaStore.Images.Media._ID + "= ?", new String[]{sImageId});

android.process.media W/MediaProvider: Ignoring mutation of description from com.example.android.someapp.app



这篇 Medium 帖子描述了 how Google has changed the API for accessing and updating files ,但是只更新元数据呢?该警告似乎告诉我 Google 不再希望允许第三方应用程序使用 MediaStore,我还发现了警告的来源:
https://android.googlesource.com/platform/packages/providers/MediaProvider/+/master/src/com/android/providers/media/MediaProvider.java#2960

有谁知道为什么更新无法在 Android 10 上运行,正确的解决方法是什么?

最佳答案

好的,在 joakimk 的帮助下,我找到了问题所在。

要更新单个内容,您需要使用 Uri指向那个单独的内容:

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DESCRIPTION, text);

    Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imgId);

    int res = getContentResolver().update(uri, values, null, null);

这种形式的update()将抛出 RecoverableSecurityException .你可以捕获它和raise a system dialog这应该会授予您成功更新此内容的权限。

基本上,决定是否抛出 RecoverableSecurityException 的逻辑取决于 Uri它本身具有内容的 ID,而不是在 WHERE 中条款。这样做的副作用是您一次不能修改多个内容,尽管新的 Android R API 可能会有所帮助。

我在 Android 10 和 Android R DP1 上对此进行了测试。

关于android - 无法在 Android 10 上更新 MediaStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366394/

相关文章:

android - 如何在 Android 中将 showSeekBarValue 设置为 true?

java - Mediastore.Audio.Media 获取最近添加的歌曲,类似于 native 播放器

android - 使用视频的 ID (Mediastore) 获取视频的 URI

android - Xamarin Android 10 安装 APK - 找不到处理 Intent 的 Activity

android - chris Jenx for Android Q 的书法库崩溃

android - 如何在 Android Q 上将照片广播到图库

java - android.content.ActivityNotFoundException : No Activity found to handle Intent

android - 为自定义 ListView 设置 onClicks

android - android.support.v7.widget 中缺少 RecyclerView

java - 如何设置 MEDIASTORE 以从 android 上的特定文件夹获取图像