Android:如何删除内部图像文件

发布于 2024-09-13 22:02:26 字数 1107 浏览 11 评论 0原文

我想要做什么:从我的应用程序的私人内部存储中删除图像文件。我将图像保存在内部存储中,以便在卸载应用程序时将其删除。

我已成功创建并保存:

String imageName = System.currentTimeMillis() + ".jpeg";
FileOutputStream fos = openFileOutput(imageName, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 35, fos);

我通过接收到的图像

bitmap = BitmapFactory.decodeStream(inputStream);

我可以稍后检索该图像以供显示:

FileInputStream fis = openFileInput(imageName);
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
DataOutputStream outWriter = new DataOutputStream(bufStream);

int ch;
while((ch = fis.read()) != -1)
    outWriter.write(ch);

outWriter.close();
byte[] data = bufStream.toByteArray();
bufStream.close();
fis.close();

imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

我现在想永久删除此文件。我尝试创建一个新文件并删除它,但找不到该文件:

File file = new File(imageName);
file.delete();

我在 android 开发者网站上读到,我必须使用 openFileInput(...) 方法打开私有内部文件,该方法返回一个 InputStream 允许我读取内容,我并不真正关心 - 我只想删除它。

谁能指出我删除存储在内部存储中的文件的正确方向?

What i want to do: delete an image file from the private internal storage in my app. I save images in internal storage so they are deleted on app uninstall.

I have successfully created and saved:

String imageName = System.currentTimeMillis() + ".jpeg";
FileOutputStream fos = openFileOutput(imageName, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 35, fos);

an image that i receive through

bitmap = BitmapFactory.decodeStream(inputStream);

I am able to retrieve the image later for display:

FileInputStream fis = openFileInput(imageName);
ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
DataOutputStream outWriter = new DataOutputStream(bufStream);

int ch;
while((ch = fis.read()) != -1)
    outWriter.write(ch);

outWriter.close();
byte[] data = bufStream.toByteArray();
bufStream.close();
fis.close();

imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

I now want to delete this file permanently. I have tried creating a new file and deleting it, but the file is not found:

File file = new File(imageName);
file.delete();

I have read on the android developer website that i must open private internal files using the openFileInput(...) method which returns an InputStream allowing me to read the contents, which i don't really care about - i just want to delete it.

can anyone point me in the right direction for deleting a file which is stored in internal storage?

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

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

发布评论

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

评论(1

时光礼记 2024-09-20 22:02:26

呃,我自己找到了答案。答案也很简单:(

您所要做的就是调用 deleteFile(imageName) 方法。

if(activity.deleteFile(imageName))
    Log.i(TAG, "Image deleted.");

完成!

Erg, I found the answer myself. Simple answer too :(

All you have to do is call the deleteFile(imageName) method.

if(activity.deleteFile(imageName))
    Log.i(TAG, "Image deleted.");

Done!

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