Android file delete leaves empty placeholder in Ga

2019-01-11 11:02发布

I insert an image via:

    ContentValues values = new ContentValues();   
    values.put(Images.Media.TITLE, filename);
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri uri = this.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

But when I try to delete

    File f = new File(imageURI);
    f.delete();

The picture is no longer there but an empty placeholder is. Any ideas?

8条回答
男人必须洒脱
2楼-- · 2019-01-11 11:09

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>
查看更多
beautiful°
3楼-- · 2019-01-11 11:12

taken from: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html

This worked for me ( tested on HTC Desire, Android 2.3)

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
查看更多
你好瞎i
4楼-- · 2019-01-11 11:14

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.

查看更多
倾城 Initia
5楼-- · 2019-01-11 11:20

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

查看更多
别忘想泡老子
6楼-- · 2019-01-11 11:28

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();
查看更多
Bombasti
7楼-- · 2019-01-11 11:29

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.

查看更多
登录 后发表回答