Android file delete leaves empty placeholder in Gallery

后端 未结 9 688
再見小時候
再見小時候 2020-12-03 05:27

I insert an image via:

    ContentValues values = new ContentValues();   
    values.put(Images.Media.TITLE, filename);
    values.put(Images.Media.DATE_ADDE         


        
相关标签:
9条回答
  • 2020-12-03 05:42

    Android has a cache of sorts that keeps track of media files.

    Try this:

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    

    It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.

    Looks like you also need to add this permission to your AndroidManifest.xml:

    <intent-filter>
      <action android:name="android.intent.action.MEDIA_MOUNTED" />
      <data android:scheme="file" /> 
    </intent-filter>
    
    0 讨论(0)
  • 2020-12-03 05:43

    Use this method getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );

    0 讨论(0)
  • 2020-12-03 05:47

    As stated in the comments, the accepted answer uses a lot of memory. While MediaScannerConnection doesn't have a "deleteFile" method, just pass in the the old file path to the "scanFile" method once you've deleted the file. The media scanner will rescan and remove the media.

    tested on N5. Android 4.4.

    EDIT: Other have stated this doesn't work on 4.2

    new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                String filePath = recording.file.getAbsolutePath();
                recording.file.delete();
                MediaScannerConnection.scanFile(context,
                        new String[]{filePath}, null, null);
                return null;
            }
        }.execute();
    
    0 讨论(0)
  • 2020-12-03 05:47

    If you donot know the image-id as required in the answer of @manisha and @Muhammad Waqas Khan you can also delete by file name

        String fullPathToFile = "/storage/sdcard0/DICM/test.jpg";
        getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            MediaStore.Images.Media.DATA + "=?", new String[]{ fullPathToFile } );
    

    [Update 2016-06-16]

    Danger: this looks like "Only delete the item from the mediaDB". Since there is a SQLite-cascade-file-delete-trigger in the mediaDB the referenced file is also deleted if it exist.

    To avoid this you have to change the column MediaStore.Images.Media.DATA to a non-existing filepath before calling delete.

    0 讨论(0)
  • 2020-12-03 05:49

    You can also delete the image from the Gallery by using:

    getContentResolver().delete(imageUri, null, null);

    Just make sure that 'imageUri' is the uri returned to you when you called insert.

    0 讨论(0)
  • 2020-12-03 06:00

    Add this line after File.delete();:

    MediaScannerConnection.ScanFile(this,new string[]{fileUrl.ToString()}, null, null);
    
    0 讨论(0)
提交回复
热议问题