0

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.

Community
  • 1
  • 1
Trabool
  • 119
  • 3

1 Answers1

0

**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.

  • As I told, _Environment.getExternalStorageDirectory()_ doesn't return the SD card path but `/storage/emulated/0`, which is internal storage for my device. You can read [this post](http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location) that explains the fact that the method does not always return SD card location. Anyway, AFAIK retrieving the path is not really the problem. – Trabool Apr 11 '17 at 11:27
  • `This will give you correct path of your sdcard.`. No. This will never give the path to an SD card. Maybe on Android 2.2. But not later. I wonder who upvoted you. – greenapps Apr 11 '17 at 12:24