Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to delete internal image file

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?

like image 834
james Avatar asked Aug 10 '10 17:08

james


1 Answers

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!

like image 91
james Avatar answered Sep 17 '22 18:09

james