简体   繁体   中英

Can't create application directory on SD card

I'm developing an app that should store photos on SD card. First the SD card path is retrieved in the code, since getExternalStorageDirectory() doesn't return the accurate SD card path. Then I'm trying to create a directory to store the pictures:

String PACKAGE_NAME = "com.example.me.sdapp";
// sdpath = "/storage/extSdCard/"
String path = sdpath+"Android/data/"+PACKAGE_NAME;
File folder = new File(path);
if (!folder.exists()){
   var = folder.mkdirs();
   if (!folder.canWrite()){
       Log.d("SD Card app", "Directory is not writable");
   }
}

The directory is not built and the log message does appear. Hence I guess I can't write to that space. However, I can create manually the directory when I run with adb shell:

mkdir /storage/extSdCard/Android/data/com.example.me.sdapp

And after creating the directory manually, the pictures can be stored in it from the app.

Why can't I create my application folder on the SD card from the app, and yet once the folder has been created manually, I can still write files to it from the app? And how could I achieve creating that folder from the application?

I have tried to make it work with and without the permission in the manifest:

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

But I guess that wouldn't be the problem anyway since as soon as I manually create the directory, the app is able to write in it.

The device is a Samsung Galaxy Tab III and Android version is 4.4.2.

EDIT

As some people marked my question as duplicate of this one , I will try to explain why that other post doesn't address my issue.

  • All the answers refer to the method getExternalStorageDirectory() which does not always point to SD card; in my case, it points to internal storage. Hence when I use this method, I can indeed create my app folder from the application, but the folder will be created in internal storage and not on SD card.

  • Some answers mention the manifest permission, which I said I have taken into account already.

**For Path retrieving **

File direct = new File(Environment.getExternalStorageDirectory() + "Your folder path" );
       if (!direct.exists()) {
                              mkdir();
                                         }

This will give you correct path of your sdcard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM