Search code examples
androidandroid-contentproviderandroid-contentresolver

Try to delete a file in android. file deleted but the file path is exist in content resolver. i did the wrong way?


I tried this code

File file = new File("/storage/emulated/0/Movies/Instagram/VID_42790208_180323_463.mp4");
file.delete();

actually its deleted the file. but the content resolver still querying the deleted file path. also i am getting the path from content resolver. help me.

thanks a lot.


Solution

  • You have to let the media scanner know that you have deleted the file.

    Try the following code after delete.

       MediaScannerConnection.scanFile(mContext, new String[]{file.getPath()}, new String[]{file.getName()},
                        new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String s, Uri uri) {
                        getContentResolver().delete(uri, null, null);
                    }
                });
    
    

    Refer the documentation for more info