On Android 11 (API30) and above, after renaming media files on a removable micro-sd card, MediaStore can‘t be updated through the ContentResolver.update() method. my targetSdkVison is 28.

I am not good at English, hope I can express myself clearly

this is my code:


String oldPath = StorageKit.getSDcardPath()+"/Pictrues/20740534.jpg"; // oldPath:/storage/0COB-3B10/Pictrues/20740534.jpg;
String newPath = StorageKit.getSDcardPath()+"/Pictrues/travelPlan.jpg"; // newPath:/storage/0COB-3B10/Pictrues/travelPlan.jpg;

File img = new File(oldPath);
File file = new File(newPath);

boolean success = img.renameTo(file); // result is true

if (success) {
    Uri uri= MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
    // result of "uri.toString()" is "content://media/external/images/media"
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    // file.getAbsolutePath():/storage/0COB-3B10/Pictrues/travelPlan.jpg;
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");
    values.put(MediaStore.Images.Media.DISPLAY_NAME, file.getName());
    values.put(MediaStore.Images.Media.TITLE, getForeString(file.getName(), '.'));
    contentResolver.update(uri, values, "_data=?", new String[]{oldPath});
}
0

There are 0 best solutions below