How to Delete Files Programmatically on Android

How do I delete files programmatically on Android?

Why don't you test this with this code:

File fdelete = new File(uri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + uri.getPath());
} else {
System.out.println("file not Deleted :" + uri.getPath());
}
}

I think part of the problem is you never try to delete the file, you just keep creating a variable that has a method call.

So in your case you could try:

File file = new File(uri.getPath());
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}

However I think that's a little overkill.

You added a comment that you are using an external directory rather than a uri. So instead you should add something like:

String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/images/media/2918");

Then try to delete the file.

can't delete file from external storage in android programmatically

public static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");

contentResolver.delete(filesUri, where, selectionArgs);

if (file.exists()) {

contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}

How to delete internal storage file in android?

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

Delete file from internal storage

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

Android 11.0: How to delete a file with Storage Access Framework?

After much research, I can provide an answer to my problem.

My application aims to list all duplicate files, and delete them through a user interface.

For the media files, I did not encounter any great difficulties, the Mediastore API is relatively easy to use.

However, for non-media files it was very complicated. Indeed, I first managed to list them via the mediastore API, but I quickly realized that it was not possible to delete them with this API.

So I then opted for saf (storage access framework), but after several rereading of the doc, I realized that I could not access the Download directory. Directory essential for the proper functioning of my application.

Then, I then tried to combine both the Mediastore API and the File API. I recovered the path of the files via mediastore, and I deleted them via the File API. However, small problem, the files were deleted from my app fine, but I could still access and view them through google's Files app.

Back to the wall, I didn't know how to get out of it. Being new to android I have no experience (this is the first application I made). But I still found a "trick" to delete a non media file:

getContentResolver().delete(path, null, null);

This line of code allows me to delete a file (both from my application, and from google's Files application). However, I have no idea whether this is a bad practice or not at all. I also saw that I can use this to delete a file:

getApplicationContext().deleteFile(filename);

But I still don't know if this is a bad practice.

Here is my journey to "solve" this problem: how to delete a non media file without using saf (storage access framework) (because unable to access the Download directory)?

If you have any remarks, advice or other, I am interested. I'm also curious if you have any other techniques to solve my problem, and if my solution is good or bad.

Be indulgent, i'm new on android, and I code on android 11 (android R) only.

Thank you very much for your reading.

How to delete file in android?

You can delete files with folder like as below,

void deleteFiles(Context context) {
File rootFolder = context.getFilesDir();
File fileDir = new File( rootFolder,getAlbumId());
if (fileDir.exists()) {
File[] listFiles = fileDir.listFiles();
for (File listFile : listFiles) {
if (!listFile.delete()) {
System.err.println( "Unable to delete file: " + listFile );
}
}
}
rootFolder.delete();
}

Source : How to delete a whole folder and content?

Don't forgot to give Storage permission.



Related Topics



Leave a reply



Submit