wilddev
wilddev

Reputation: 1964

Deleted file resurrects after ACTION_MEDIA_SCANNER_SCAN_FILE intent

I have to delete an image in my application. When I use File.delete method (returns true, mean successfully deleted), file is deleted on file system, but it's visible in gallery. To delete it from gallery I use sending of ACTION_MEDIA_SCANNER_SCAN_FILE intent or calling MediaScannerConnection.scanFile.

After that the strange thing happens: deleted file resurrects and returns in File.listFiles method.

How do I delete the file both from FileSystem and Gallery?

Upvotes: 0

Views: 963

Answers (1)

krystian71115
krystian71115

Reputation: 1937

Delete it from database:

import android.provider.MediaStore;
import android.content.Context;
import android.content.ContentResolver;

// if you calling this method from an Activity pass context parameter as this
public void deleteFromDatabase(Context context, File file)
{
    Uri contentUri = MediaStore.Images.Media.getContentUri("external");
    ContentResolver resolver = context.getContentResolver();
    int result = resolver.delete(contentUri, MediaStore.Images.ImageColumns.DATA + " LIKE ?", new String[]{file.getPath()});
    if(result > 0){
        // success
    } else {
        // fail or item not exists in database
    }
}

Upvotes: 3

Related Questions