4

I made a little file explorer in my app and I want to support creating new folders.

When navigating to a location in the device's built in memory, everything works as expected but when I navigate to my sd card and try to create a new folder, the line new File(path + File.separator + newFolderName).mkdirs() returns false.

I've made the following debug tests:

String path = "/storage/external_SD";   // Not hard coded - user navigates here
new File(path).isDirectory();            --> true
new File(path).canRead();                --> true
new File(path).canWrite();               --> true

String newFolderPath = path + File.separator + newFolderName;
new File(newFolderPath).isDirectory();   --> false
new File(newFolderPath).mkdir();         --> false
new File(newFolderPath).mkdirs();        --> false

Something odd I've noticed: I put a breakpoint in the File class on the first line of mkdirs() but the application didn't stop on the line and immediately returned false. This made me think that's a permissions issue even though I have the correct permission in the right location:

<manifest ...>

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

    <application ...>
    ...
    </application>

</manifest>

What am I missing?

iMax531
  • 177
  • 1
  • 2
  • 15
  • 1
    Just a question for I am not sure, your path is `/storage/external_SD/` correct? Now your newFolderPath is equal to `path + File.separator + newFolderName`. You already have a seperator at the end of your path and you insert another one by using File.separator. Could this be the issue? – George Daramouskas Jan 29 '16 at 14:25
  • @GeorgeD my bad, the path String doesn't have that last separator. I fixed the question – iMax531 Jan 29 '16 at 14:28
  • Can you post the value of newFolderPath ? – George Daramouskas Jan 29 '16 at 14:30
  • @GeorgeD the value is "/storage/external_SD/folder_name" – iMax531 Jan 29 '16 at 14:50

1 Answers1

1

Since Android 5.0, an application may only write to it's private location on an external SD-card.

For all other locations, it needs to implement a document provider.

The strategy to access external storage is outlined in this Google documentation.

Martin C.
  • 12,140
  • 7
  • 40
  • 52
  • according to the documentation, I'm free to manipulate files and folders in public locations, which is what I'm trying to do, don't I? "Files saved to the external storage are world-readable". All I need is the permission I already have. – iMax531 Jan 29 '16 at 14:54
  • Yes, but since 5.0, Android doesn't consider the ext-SD this way, only a special directory inside the card with the package's name. I'll try to find some official documentation of this, I'm just talking from a lot of tickets on a lot of bug trackers. Note, that an _internal_ emulated SD card is not restricted this way. – Martin C. Jan 29 '16 at 14:56
  • @iMax531 This Stackoverlow disucssion outlines everything quite well in my opinion: http://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-lollipop – Martin C. Jan 29 '16 at 14:58