Delete file from internal storage

AndroidFile Io

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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();

Solution 2 - Android

Have you tried Context.deleteFile() ?

Solution 3 - Android

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)

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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);

Solution 7 - Android

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.

Solution 8 - Android

>     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 ();
            }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCrunchView Question on Stackoverflow
Solution 1 - AndroidCrunchView Answer on Stackoverflow
Solution 2 - AndroidKonstantin BurovView Answer on Stackoverflow
Solution 3 - AndroidJiyehView Answer on Stackoverflow
Solution 4 - AndroidVikingGlenView Answer on Stackoverflow
Solution 5 - Androidmvsjes2View Answer on Stackoverflow
Solution 6 - Androidsweet_vishView Answer on Stackoverflow
Solution 7 - AndroidRonnieView Answer on Stackoverflow
Solution 8 - AndroidNenad ŠtrbićView Answer on Stackoverflow