2

I have a peice of code that scans for all files in a directory and it should delete those files. But for some reason it's not deleting them.

What I have is this:

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Images/"; 
File f = new File(path);
File file[] = f.listFiles();
for (File aFile : file) {
    boolean isDeleted = aFile.delete();
    if(isDeleted) {
        log.d("file", "is deleted");
    }
}

When I debug this code then it says for every file that isDeleted is true. But when I check the "Gallery/Images" folder on my phone I see that all images are still there...

I also have the following two permission in my manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Anyone any idea why the files aren't deleted, eventhough it says isDeleted is true?

Vivendi
  • 20,047
  • 25
  • 121
  • 196
  • @greywolf82 Mediascanner? Not sure what you mean? Also note, the files that I want to delete are strored in a folder from another app. – Vivendi Dec 06 '14 at 10:59
  • You can't do this in KitKat. You must have root. – Simon Dec 06 '14 at 11:21
  • @Simon it's not true. In Kitkat the problem is only with secondary external storage. In the OP code is clear that the access is done in primary external storage. – greywolf82 Dec 06 '14 at 11:55

1 Answers1

3

You can't delete files anymore in Android 4.4.2. At least, not files that were created by other apps. This is a new (and dumb) restriction.

What you have to do is Root your phone and then install an app like SDFix or something similar. That you way you gain full access to your SD Card again.

That's currently the best way.

Or you could try a "loophole", as in this post:

How to delete a file from SD card?

Community
  • 1
  • 1
w00
  • 26,172
  • 30
  • 101
  • 147
  • Why dumb? It gets my vote as going someway to controlling the complete chaos that was my SDCard in the past and is definitely a security enhancement. What's wrong with it? – Simon Dec 06 '14 at 11:21
  • The OP question is not about the SD card just because the method getExternalStorageDirectory() doesn't give the path on any external SD card but it gives you only the "internal" storage path (I know the name is not the better choice done by Google). – greywolf82 Dec 06 '14 at 11:25
  • @greywolf82 What ever. SD / internal storage. This is the problem he is facing and those are the only two solutions for it. So I don't understand the down vote. – w00 Dec 06 '14 at 11:26
  • 1
    Apps must not be allowed to write to **secondary** external storage devices. – greywolf82 Dec 06 '14 at 11:31
  • @greywolf82 That's your opinion (and Google's). But that's besides the point. He wants to know why the files aren't being deleted and I simply tell him why, with some possible solutions. – w00 Dec 06 '14 at 12:10
  • Actually, this IS exactly the problem I'm facing. It indeed seems that Google blocked the "File Writing" permissions. The app will only be used by me. It will function as some sort of "server processing thing". I just did a Root and installed the app you suggested. Everything works like a charm. And it was done within a few minutes! – Vivendi Dec 06 '14 at 12:12