I want to delete all files from Download folder
I am trying this approach -
File mydir = new File(String.valueOf(Environment.getExternalStoragePublicDirectory("Download")));
File lister = mydir.getAbsoluteFile();
System.out.println("Total files: " + lister.list().length);
for (String list : lister.list()) {
File f = new File(lister, list);
if (f.delete())
System.out.println(list + " is Deleted!");
else
System.out.println(list + " not deleted!");
}
It doesn't work, f.delete is returning false.
I have already looked at many such questions on SO, most of them suggest the use of delete() or getCanonicalFile().delete(). This is just not working.
manifest-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Fixed it.
My app was requesting the user for READ permission only, not WRITE.
Deleting any file requires WRITE permission. So i changed READ to WRITE, it worked!
if (ContextCompat.checkSelfPermission(MainActivity.this
, Manifest.permission.**WRITE**_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
// ...
}