6

I can't delete file from sdcard.

File toDelete = new File(fname);
boolean result=toDelete.delete();

The result is false. Reads and writes of the same file is working in same app running. No opened streams. No exceptions raised. I have tried to make it writable just before deletion this way

toDelete.setWritable(true);

with no effect. How does it possible that the system can write and read, but can not delete the same file???

Ricardo A.
  • 685
  • 2
  • 8
  • 35

2 Answers2

3

use below code it may help you.

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

Refresh gallery after delete image

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
 Uri.parse("file://" +  Environment.getExternalStorageDirectory())));

check this: https://stackoverflow.com/a/10716773/1168654

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
1

Try doing this

 File fileToDelete = new File(YourPath);
 boolean deleted = fileToDelete.delete();

Remount your card again and check

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                     Uri.parse("file://"
                               + Environment.getExternalStorageDirectory())));
Sanket Pandya
  • 1,095
  • 7
  • 21