Android:从 SD 卡中删除图像

发布于 2024-11-20 00:04:38 字数 979 浏览 2 评论 0原文

我需要从用户选择的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

因为看清所以看轻 2024-11-27 00:04:38

你为什么要把它搞得那么复杂?

你可以像这样简单地做到这一点:

getContentResolver().delete(Utils.imgUri, null, null);

Why would you make it that complex?

You can do it as simple as this:

getContentResolver().delete(Utils.imgUri, null, null);
南…巷孤猫 2024-11-27 00:04:38

已解决的添加:

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

它使 MediaScanner 服务再次运行,这应该从设备的缓存中删除已删除的图像。

同样的问题

Resolved adding:

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.

same problem here

述情 2024-11-27 00:04:38

您是否在清单文件中设置了 权限?

Have you set the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission in the manifest file?

Saygoodbye 2024-11-27 00:04:38

您确定文件路径正确吗?因为实际删除的方式应该没问题,请参阅此线程:
如何从 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)

一场信仰旅途 2024-11-27 00:04:38

在参数“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).

春花秋月 2024-11-27 00:04:38

简单的一行;)

 new File(uri.getPath()).delete();

并且在清单中必须使用这些权限

android.permission.WRITE_EXTERNAL_STORAGE

simple one line ;)

 new File(uri.getPath()).delete();

and in manifest must use these permissions

android.permission.WRITE_EXTERNAL_STORAGE

じее 2024-11-27 00:04:38

onDestroy方法中添加以下代码:

 if(myFile.exists())
 myFile.delete();

并且不要忘记在Manifest文件

android.permission.WRITE_EXTERNAL_STORAGE中添加权限

add below code in onDestroy method:

 if(myFile.exists())
 myFile.delete();

and don't forget to add permission in Manifest file

android.permission.WRITE_EXTERNAL_STORAGE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文