Removing a file from internal storage

I am trying to delete images stored in internal storage. I came up with this so far:

File dir = getFilesDir(); File file = new File(dir, id+".jpg"); boolean deleted = file.delete(); 

And this is from another question that was answered with :

 File dir = getFilesDir(); File file = new File(dir, "my_filename"); boolean deleted = file.delete(); 

My example always returns false. I see the fx 2930.jpg file in DDMS in eclipse.

+62
android file-io
Mar 30 '11 at 12:53
source share
6 answers

getFilesDir() somehow did not work. Using a method that returns the entire path and file name, gave the desired result. Here is the code:

 File file = new File(ih.getImgPath(id)); boolean deleted = file.delete(); 
+86
Mar 30 '11 at 13:01
source share

Have you tried Context.deleteFile () ?

+40
Mar 30 2018-11-11T00:
source share
 String dir = getFilesDir().getAbsolutePath(); File f0 = new File(dir, "myFile"); boolean d0 = f0.delete(); Log.w("Delete Check", "File deleted: " + dir + "/myFile " + d0); 

Code File dir = getFilesDir(); does not work because it is a request for a File object. You are trying to get a string declaring the path to your directory, so you need to declare "dir" as String, and then request that the absolute directory path in the form of String be returned by the constructor that has access to this information.

+11
Jul 21 '14 at 4:26
source share

This works for me:

 File file = new File(photoPath); file.delete(); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoPath)))); 
+9
Sep 16 '15 at 14:27
source share

You can also use: file.getCanonicalFile().delete();

+8
Jan 23 2018-12-23T00:
source share

Have you tried getFilesDir().getAbsolutePath() ?

You seem to have fixed your problem by initializing the File object in the full path. I believe that will help too.

+2
Aug 14 '13 at 20:08
source share



All Articles