12

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

我已成功创建并保存:

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 的方法打开私有内部文件,该方法允许我读取内容,我并不真正关心 - 我只想删除它。

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

4

1 回答 1

25

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

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

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

完毕!

于 2010-08-10T18:05:45.497 回答