无法在Android 10上更新MediaStore [英] Can't update MediaStore on Android 10
问题描述
我一直在通过ContentResolver更新MediaStore中的元数据,但是这不再适用于Android Q(API 29).以下代码向我发出警告,并且说明未更新:
I've been updating meta data in the MediaStore through a ContentResolver, but this no longer works with Android Q (API 29). The following code gives me a warning, and the description is not updated:
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:忽略的突变com.example.android.someapp.app中的说明
android.process.media W/MediaProvider: Ignoring mutation of description from com.example.android.someapp.app
这篇中篇文章介绍了 Google如何更改访问和更新文件的API ,但是仅更新元数据又如何呢?该警告似乎告诉我Google不再希望允许第三方应用程序使用MediaStore,而且我还发现警告来自何处: https://android.googlesource.com/platform/packages/providers/MediaProvider/+/master/src/com/android/providers/media/MediaProvider.java#2960
This Medium post describes how Google has changed the API for accessing and updating files, but what about updating just the meta data? The warning seems to tell me Google no longer wants to allow third party apps to use the MediaStore, and I also found where the warning comes from: https://android.googlesource.com/platform/packages/providers/MediaProvider/+/master/src/com/android/providers/media/MediaProvider.java#2960
有人知道为什么更新不能在Android 10上运行吗,什么是适当的解决方法?
Does anyone know why the update is not working on Android 10, and what is the proper workaround?
推荐答案
好的,在joakimk的帮助下,我找到了问题.
OK, with the help of joakimk, I found the problem.
要更新单个内容,您需要使用指向该单个内容的 Uri
:
To update an individual piece of content, you need to use a Uri
that points to that individual piece of content:
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
.您可以抓住它并引发系统对话框应该授予您成功更新此内容的权限.
This form of update()
will throw a RecoverableSecurityException
. You can catch that and raise a system dialog that should give you the permission to successfully update this content.
基本上,决定是否引发 RecoverableSecurityException
的逻辑取决于具有内容ID的 Uri
本身,而不是位于 WHERE中的内容
子句.这样做的副作用是,您一次不能修改多个内容,尽管新的Android R API可能会有所帮助.
Basically, the logic that decides whether to throw a RecoverableSecurityException
depends on the Uri
itself having the ID of the content, rather than it being in a WHERE
clause. A side effect of this is that you cannot modify more than one piece of content at a time, though the new Android R APIs for that may help.
我在Android 10和Android R DP1上对此进行了测试.
I tested this on Android 10 and Android R DP1.
这篇关于无法在Android 10上更新MediaStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!