Can't update MediaStore on Android 10

后端 未结 1 1555
死守一世寂寞
死守一世寂寞 2021-01-05 00:48

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 d

相关标签:
1条回答
  • 2021-01-05 01:36

    OK, with the help of joakimk, I found the problem.

    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);
    

    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.

    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.

    I tested this on Android 10 and Android R DP1.

    0 讨论(0)
提交回复
热议问题