Android file delete leaves empty placeholder in Gallery

左心房为你撑大大i 提交于 2019-11-26 16:26:10

问题


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?


回答1:


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>



回答2:


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();



回答3:


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.




回答4:


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.




回答5:


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



回答6:


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




回答7:


Add this line after File.delete();:

MediaScannerConnection.ScanFile(this,new string[]{fileUrl.ToString()}, null, null);



回答8:


Add this right after deleting a file:

MediaScannerConnection.scanFile(context, arrayOf(imageURI.getPath()), null, null)

:)



来源:https://stackoverflow.com/questions/4430888/android-file-delete-leaves-empty-placeholder-in-gallery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!