android程序中如何删除刚拍的照片

发布于 2024-09-27 15:12:38 字数 305 浏览 1 评论 0原文

我编写了一个程序,该程序使用意图进行图像捕获,以使用手机中的应用程序获取照片。 使用 MediaStore.EXTRA_OUTPUT,我获得图像的 URI,该 URI 转换为路径结果,结果类似于“/external/images/media/NN”,其中 NN 是照片的编号。

现在,在我的程序中,在读取并操作图像后,我想删除该图像。

我该怎么做呢?

(File image = new File(path); image.delete(); // returns false, so doesn't work)

I have written a program that uses the Intent for the image capture to get a photo using the application in the phone.
Using MediaStore.EXTRA_OUTPUT, I get a URI to the image, which converted to a path results in something like "/external/images/media/NN" where NN is the number of the photo.

Now, in my program, after I read and manipulated the image, I want to delete that image.

How should I do that?

(File image = new File(path); image.delete(); // returns false, so doesn't work)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

尤怨 2024-10-04 15:12:38

感谢您的回答。

我决定阅读这个答案,Problems saving a photo to a file

对于对我来说,它适用于 Hero,即使在该代码的注释中被剪断说 Hero 的行为有所不同。
现在我在“/sdcard/image.tmp”中获取了图像,并且可以将其删除。
我认为这是最好的解决方案,因为我认为尝试将相机应用程序写入我的应用程序目录中很麻烦。

再次感谢。

Thanks for the answer.

I resolved reading this answer, Problems saving a photo to a file

For me it works on Hero, even if in the comment in that code snipped says that Hero behave differently.
Now I get the image in "/sdcard/image.tmp", and I can delete it.
I think this is the best solution, cause I think it's a trouble trying to get the camera app to write in my app directory.

Thanks again.

音栖息无 2024-10-04 15:12:38

是否有可能您未能在应用程序中请求 WRITE_EXTERNAL_STORAGE 权限?这会导致删除失败。 (无论如何,相机都可以写入,因为它是一个具有自己权限的单独应用程序。)

媒体文件夹也有可能(但不太可能)只能由相机应用程序写入,在这种情况下,您需要在调用相机的意图的 EXTRA_OUTPUT extra 中指定不同的所需目的地,以便将文件写入应用程序的目录中。事实上,您可能无论如何都想这样做,以避免私有资源弄乱全局空间,即使您要立即删除它们。

Is it possible that you've failed to request the WRITE_EXTERNAL_STORAGE permission in your app? This would cause deletions to fail. (The camera would be able to write regardless, since it is a separate app with its own permissions.)

It's also possible (but not likely) that the media folder is only writable by the camera app, in which case you'd want to specify a different desired destination in the EXTRA_OUTPUT extra of the intent which invokes the camera so that the file will be written into your app's directory. In fact, you probably want to do that anyway to avoid cluttering up the global space with private resources, even if you are going to delete them immediately.

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