100

I'm trying to delete images stored in internal storage. I've come 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, which was answered with:

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

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
Crunch
  • 2,823
  • 5
  • 18
  • 17

8 Answers8

142

The getFilesDir() somehow didn't work. Using a method, which returns the entire path and filename gave the desired result. Here is the code:

File file = new File(inputHandle.getImgPath(id));
boolean deleted = file.delete();
Crunch
  • 2,823
  • 5
  • 18
  • 17
48

Have you tried Context.deleteFile() ?

Real KEK
  • 169
  • 2
  • 12
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • Tried a variant with Contex.deletFile(), which didn't work. Below is what seemed to work. – Crunch Apr 01 '11 at 04:24
  • @user661543 What's full path returned by the ih.getImgPath()? What you'd passed as an argument to delete file? If the method above didn't work then most likely stored your file out of application package. Or you might have passed wrong file name as an argument. – Konstantin Burov Apr 01 '11 at 07:27
  • +1 It's simplest way to remove file written via `new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE)).write(data);` – Eugen Dec 10 '17 at 10:56
21

Java

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

MediaScannerConnection.scanFile(context,
                new String[]{file.toString()},
                new String[]{file.getName()},null);

Kotlin

val file = File(photoPath) 
file.delete()

MediaScannerConnection.scanFile(context, arrayOf(file.toString()),
      arrayOf(file.getName()), null)
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jiyeh
  • 5,187
  • 2
  • 30
  • 31
15
String dir = getFilesDir().getAbsolutePath();
File f0 = new File(dir, "myFile");
boolean d0 = f0.delete(); 
Log.w("Delete Check", "File deleted: " + dir + "/myFile " + d0);

The code File dir = getFilesDir(); doesn't work because this is a request for a File object. You're trying to retrieve the String that declares the path to your directory, so you need to declare 'dir' as a String, and then request that the directory's absolute path in String form be returned by the constructor that has access to that information.

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86
VikingGlen
  • 1,705
  • 18
  • 18
11

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

Diego Garcia
  • 555
  • 2
  • 6
  • 20
mvsjes2
  • 1,254
  • 2
  • 14
  • 23
4
File file = new File(getFilePath(imageUri.getValue())); 
boolean b = file.delete();

is not working in my case.

boolean b = file.delete();                 // returns false
boolean b = file.getAbsolutePath.delete(); // returns false 

always returns false.

The issue has been resolved by using the code below:

ContentResolver contentResolver = getContentResolver();
contentResolver.delete(uriDelete, null, null);
Floern
  • 33,559
  • 24
  • 104
  • 119
sweet_vish
  • 141
  • 1
  • 8
2

Have you tried getFilesDir().getAbsolutePath()?

Seems you fixed your problem by initializing the File object with a full path. I believe this would also do the trick.

Nayan
  • 1,521
  • 2
  • 13
  • 27
Ronnie
  • 121
  • 1
  • 3
0
>     2019-11-12 20:05:50.178 27764-27764/com.strba.myapplicationx I/File: /storage/emulated/0/Android/data/com.strba.myapplicationx/files/Readings/JPEG_20191112_200550_4444350520538787768.jpg//file when it was created

2019-11-12 20:05:58.801 27764-27764/com.strba.myapplicationx I/File: content://com.strba.myapplicationx.fileprovider/my_images/JPEG_20191112_200550_4444350520538787768.jpg //same file when trying to delete it

solution1:

              Uri uriDelete=Uri.parse (adapter.getNoteAt (viewHolder.getAdapterPosition ()).getImageuri ());//getter getImageuri on my object from adapter that returns String with content uri

here I initialize Content resolver and delete it with a passed parameter of that URI

            ContentResolver contentResolver = getContentResolver ();
            contentResolver.delete (uriDelete,null ,null );

solution2(my first solution-from head in this time I do know that ): content resolver exists...

              String path = "/storage/emulated/0/Android/data/com.strba.myapplicationx/files/Readings/" +
                    adapter.getNoteAt (viewHolder.getAdapterPosition ()).getImageuri ().substring (58);

            File file = new File (path);
            if (file != null) {
                file.delete ();
            }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Nenad Štrbić
  • 367
  • 4
  • 6