简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM