Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why file isn't being deleted with file.delete()? [closed]

Tags:

file

android

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???

like image 954
Catherine Ivanova Avatar asked Mar 29 '13 10:03

Catherine Ivanova


2 Answers

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

like image 61
Dhaval Parmar Avatar answered Nov 06 '22 12:11

Dhaval Parmar


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())));
like image 34
Sanket Pandya Avatar answered Nov 06 '22 11:11

Sanket Pandya