Android 文件删除在图库中留下空占位符
我通过以下方式插入图像:
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);
但是当我尝试删除时,
File f = new File(imageURI);
f.delete();
图片不再存在,但有一个空占位符。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Android 有某种缓存来跟踪媒体文件。
试试这个:
它使 MediaScanner 服务再次运行,这应该会从设备的缓存中删除已删除的图像。
看起来您还需要将此权限添加到 AndroidManifest.xml 中:
Android has a cache of sorts that keeps track of media files.
Try this:
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:
正如评论中所述,接受的答案使用了大量内存。虽然 MediaScannerConnection 没有“deleteFile”方法,但删除文件后只需将旧文件路径传递给“scanFile”方法即可。介质扫描仪将重新扫描并移除介质。
在N5上测试过。安卓4.4。
编辑:其他人指出这在 4.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
您还可以使用以下方法从图库中删除图像:
getContentResolver().delete(imageUri, null, null);
只需确保“imageUri”是您调用 insert 时返回给您的 uri。
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.
如果您不知道 @manisha 的答案中所需的 image-id 和
@Muhammad Waqas Khan 您还可以按文件名删除
[更新2016-06-16]
危险:这看起来像“仅从mediaDB中删除项目”。
由于 mediaDB 中有一个 SQLite-cascade-file-delete-trigger,因此引用的文件也会被删除(如果存在)。
为了避免这种情况,您必须在调用删除之前将 MediaStore.Images.Media.DATA 列更改为不存在的文件路径。
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
[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.
取自: http:// developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html
这对我有用(在 HTC Desire、Android 2.3 上测试)
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)
使用这个方法
getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
Use this method
getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
在
File.delete();
之后添加此行:Add this line after
File.delete();
:删除文件后立即添加:
MediaScannerConnection.scanFile(context, arrayOf(imageURI.getPath()), null, null)
:)
Add this right after deleting a file:
MediaScannerConnection.scanFile(context, arrayOf(imageURI.getPath()), null, null)
:)