user2553585 user2553585 - 1 month ago 10
Android Question

Android deleting the micro_kind thumbnails after image file deletion

I am populating a gridview with MICRO_KIND thumbnails using the following:

/* Find images of interest */
imagecursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CON TENT_URI,
columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[]{"%/houseTab" + currentHouseNumber + "/%"},
null);

/* Retrieve MICRO_KIND Thumbnails */
int id = imagecursor.getInt(image_column_index);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getActivity().getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);


The retrieve process works perfectly; the issue happen when I delete the actual image files I can not delete the MICRO_KIND Thumbnails. This is what I am using right now and the files images gets deleted but the MICRO_KIND does not get deleted and still visible in the gridview even after a refresh. To get rid of the thumbnail I have to turn off the device or do a unmount/mount of the sdcard.

int count = imagecursor.getCount();
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();

for (int i = 0; i < count; i++) {
new File(arrPath[i]).delete(); // Delete the actual image file
imagecursor.moveToPosition(i);

long id = imagecursor.getInt(image_column_index);

/* Delete the thumbnails ???? Not working */
cr.delete(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, MediaStore.Images.Thumbnails.IMAGE_ID +
"= ?",new String[]{"" + id});


By the way arrPath is retrieve from the mediastore using the following:

int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);


I also try to following to delete the thumbnails but also without any success.

MediaScannerConnection.scanFile(
getActivity().getApplicationContext(),
new String[]{arrPath[i]},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
refreshImages();
}
});


So how do I remove this entry from the database so the when the imagecursor is refreshed after the file deletion the imagecursor is empty and no MICRO_KIND or any data for that matter is returned???

Any help would be appreciated.

Answer

Hopefully this will help others. I manage to delete the entry from the mediastore hence the MICRO_KIND thumbnails using the following:

 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            id);
    cr.delete(uri,null,null);