2

I am deleting image after capture and doing some work on it.But after deleting the image Gallery till hold the thumbnails. How clear gallery thumbnails after deleting image from DCIM in Android.

private boolean deleteLastFromDCIM() {

boolean success = false;
try {
    File[] images = new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM"+File.separator+"Camera").listFiles();
    File latestSavedImage = images[0];
    for (int i = 1; i < images.length; ++i) {
        if (images[i].lastModified() > latestSavedImage.lastModified()) {
            latestSavedImage = images[i];
        }
    }

            // OR JUST Use  success = latestSavedImage.delete();
    success = new File(Environment.getExternalStorageDirectory()
            + File.separator + "DCIM/Camera/"
            + latestSavedImage.getAbsoluteFile()).delete();
    return success;
} catch (Exception e) {
    e.printStackTrace();
    return success;
}}


if(file.exists()){
    Calendar time = Calendar.getInstance();
    time.add(Calendar.DAY_OF_YEAR,-7);
    //I store the required attributes here and delete them
    Date lastModified = new Date(file.lastModified());
    if(lastModified.before(time.getTime()))
    {
        //file is older than a week
    }
    file.delete();
}else{
    file.createNewFile();
}

1 Answers1

3

Just you need to write a method for delete file and replace the code file.delete(); with deleteFileFromMediaStore(this.getContentResolver(),file);

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