Android:从 SD 卡中删除图像
我需要从用户选择的 SD 卡中删除图像。 在我的活动中,用户从图库中选择图像后,我执行以下代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Utils.imgUri = data.getData();
Utils.imgPath = getPath(Utils.imgUri);
File file = new File(Utils.imgPath);
boolean deleted = file.delete();
}
}
}
其中 getPath 方法是:
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
图像已正确删除,但在图库中仍然保留已删除图像的预览。 当我点击它时,会加载一个黑色图像..
那么,在从应用程序代码中删除一些图像后,如何更新图库预览?
I need to remove an image from sd card chosen by user.
In my Activity, after an user select an image from gallery, i execute this code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Utils.imgUri = data.getData();
Utils.imgPath = getPath(Utils.imgUri);
File file = new File(Utils.imgPath);
boolean deleted = file.delete();
}
}
}
where getPath method is:
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
The images are correctly removed but in the gallery still remain a preview of the removed image.
When i tap on it, is loaded a black image..
so, How can I update the gallery previews, after I delete some images from my app code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你为什么要把它搞得那么复杂?
你可以像这样简单地做到这一点:
Why would you make it that complex?
You can do it as simple as this:
已解决的添加:
它使 MediaScanner 服务再次运行,这应该从设备的缓存中删除已删除的图像。
同样的问题
Resolved adding:
It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.
same problem here
您是否在清单文件中设置了
权限?Have you set the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
permission in the manifest file?您确定文件路径正确吗?因为实际删除的方式应该没问题,请参阅此线程:
如何从 SD 卡中删除文件?
应该 / mnt/真的在那里吗?另外,您是否有权从存储中删除文件? (android.permission.WRITE_EXTERNAL_STORAGE)
Are you sure that the file path is correct? Because the way you do the actual delete should be fine, see this SO thread:
How to delete a file from SD card?
Should the /mnt/ really be there? Also, do you have the permissions to delete files from the storage? (android.permission.WRITE_EXTERNAL_STORAGE)
在参数“data”中,您也有 Uri,只需执行“
data.getUri()
”即可。另外,您是否在真实设备中进行测试?如果是这样并且如果是三星,则不起作用(请参阅此in the parameter "data" you have the Uri too, just do "
data.getUri()
". Also, are you testing in a real devices? if so and if it is a samsung, it isn't work (see this thread).简单的一行;)
并且在清单中必须使用这些权限
android.permission.WRITE_EXTERNAL_STORAGE
simple one line ;)
and in manifest must use these permissions
android.permission.WRITE_EXTERNAL_STORAGE
在onDestroy方法中添加以下代码:
并且不要忘记在Manifest文件
android.permission.WRITE_EXTERNAL_STORAGE
中添加权限add below code in onDestroy method:
and don't forget to add permission in Manifest file
android.permission.WRITE_EXTERNAL_STORAGE