Cannot create directory in external storage although permissions are apparently set correctly

為{幸葍}努か 提交于 2019-12-10 01:30:09

问题


I have

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

in my manifest file, however I fail when trying to create a directory

    Log.d(LOG_STRING, android.os.Environment.getExternalStorageState() );
    java.io.File folder = new java.io.File(Environment.getExternalStorageDirectory() + java.io.File.separator + "test");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        Log.d(LOG_STRING, "Created directory");
    } else {
        Log.d(LOG_STRING, "FAILED WHILE CREATING DIRECTORY");
    }

The status of external storage is "mounted", but the test directory cannot be created and the output is "FAILED WHILE CREATING DIRECTORY".

Browsing in the phone to the "App Info", the permission "modify or delete the contents of your USB storage" is marked to be activated for my application.

What could be the cause of this? Some special setting of the phone? It's a Samsung GT-I9506 with Android 4.3 (API18). To be noted is that the getExternalStorageDirectory is not on the SD card, but on the internal storage (/storage/emulated/0/).


Update:

Speaking with colleagues, it seems that this device has undergone several tweaking after having been rooted (to allow a specific application to directly write on the SD card). It's probably not worth to investigate further, I will simply switch to another device. I'll keep the device for a while and if anybody will show up with an answer I will quickly test if it solves the problem.


Update 2: (Bounty end)

The problem remains unsolved, but as stated before, it is most likely something very specific to this one device. It's not possible to write on any path, being it external or internal storage, not even in the path returned by getExternalCacheDir().


回答1:


In my case I had to go under Settings -> Apps -> NameOfApp -> Permissions and activate the Storage permissions. I don't know why this comes deactivated by default if I have all the permissions on AndroidManifest.xml. I'm on a Moto G (3rd Gen) with Android Marshmallow 6.0.




回答2:


You first need to use file.mkdirs() instead of folder.mkdir().

And if it also not worked then you need to check if you can access the sdcard or not.

For checking you can use below method.

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;

} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;

} else {

    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;

}

Read at official document .

Hope it will give some clue to you.




回答3:


You can try this code. I have also emulated sd card on Lg g2 mini and I can only access external card in my package folder

for(File f : getExternalFilesDirs(null)){
    try {
        File f2 = new File(f.getAbsolutePath(), "testFile");

        Log.d("test log", "file path " + f2.getAbsolutePath());
        f2.createNewFile();
        Log.d("test log", "file exist " + f2.exists());
    } catch (IOException e) {
        Log.d("test log", "error");
    }
}

and output is:

D/test log? file path /storage/emulated/0/Android/data/my.awesome.package/files/testFile
D/test log? file exist true
D/test log? file path /storage/external_SD/Android/data/my.awesome.package/files/testFile
D/test log? file exist true



回答4:


Try this:

     final File externalStoragePublicDirectory = Environment.getExternalStorageDirectory();
     File file = new File(externalStoragePublicDirectory, "test");
     file.mkdirs();


来源:https://stackoverflow.com/questions/33305100/cannot-create-directory-in-external-storage-although-permissions-are-apparently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!