Android: how to delete internal image file

我与影子孤独终老i 提交于 2020-01-12 05:10:29

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/3451896/android-how-to-delete-internal-image-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!