Android Question How to delete thumbnails from MediaStore?

Inman

Well-Known Member
Licensed User
Longtime User
In my app I modify some existing images (like rotating, flipping). While the image is saved correctly, the thumbnails in apps like Google Photos and Gallery still show the old photos. I read that this is because MediaStore still has the old thumbnails which need to be deleted once you modify the file.

I found a Java code to delete thumbnails from MediaStore. Can you please translate the code to B4A so that I can delete the thumbnail by passing it's directory and filename as parameters?
B4X:
public  void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
    String canonicalPath;
    try {
        canonicalPath = file.getCanonicalPath();
    } catch (IOException e) {
        canonicalPath = file.getAbsolutePath();
    }
    final Uri uri = MediaStore.Files.getContentUri("external");
    final int result = contentResolver.delete(uri,
            MediaStore.Files.FileColumns.DATA + "=?", new String[] {canonicalPath});
    if (result == 0) {
        final String absolutePath = file.getAbsolutePath();
        if (!absolutePath.equals(canonicalPath)) {
            contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
        }
    }
}
 

Inman

Well-Known Member
Licensed User
Longtime User
Thanks Erel. Based on your instructions it was easy to implement.
B4X:
Sub UpdateMediaStore(dirname As String,filename As String)
    Dim cr As ContentResolver, uri As Uri, path As String
    
    path=File.Combine(dirname,filename)   
    uri.Parse("content://media/external/file")
    
    cr.Initialize("")
    cr.Delete(uri,"_data" & "=?",Array As String(path))       
End Sub
 
Upvote 0
Top