Android failed to mkdir() on SD card

守給你的承諾、 提交于 2019-12-08 07:27:12

问题


I have some code to create/get a folder on SD card:

if( hasSDCard() ){

        UUID uniqueFileName = UUID.randomUUID();

        mediaStorageDir = new File(
                getExternalImageStoragePath(),
                "MyApp");

        if ( ! mediaStorageDir.exists() ){

            if( ! mediaStorageDir.mkdirs() ){

                MyLogger.Error("Create image directory FAILED. path: " + mediaStorageDir.getPath());

                return null;
            }
        }

and I have the permission registered in my Manifest file:

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

but when I run it, it prints:

Create image directory FAILED. path: /storage/emulated/0/Pictures/MyApp

I was wondering why this happens?

=== UPDATE ===

I have tried all of them:

/**
 * get external storage directory path for image
 * @return
 */
public static String getExternalImageStoragePath(){

    String strPath = "";

    if(hasSDCard()){
        //strPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
        //strPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
        //strPath = Environment.getExternalStorageDirectory().getPath();
        strPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    return strPath;
}

But they all behave exactly the same...

=== UPDATE 2 ===

I am testing with Nexus 5. I saw this post Cannot find storage/emulated/0/ folder of Nexus 7 in Eclipse There might be some issue?

=== UPDATE 3 ===

>> adb shell ls -l /storage/
dr-xr-xr-x root     root              1970-01-24 23:48 emulated
lrwxrwxrwx root     root              1970-01-24 23:48 sdcard0 -> /storage/emulated/legacy

>> adb shell ls -l /storage/emulated/0/Pictures/
/storage/emulated/0/Pictures/: No such file or directory

=== UPDATE 4 ===

Here is my hasSD() method:

public static boolean hasSDCard(){
    boolean fHasSDCard = false;

    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        fHasSDCard = true;
    }

    return fHasSDCard;
}

回答1:


EDIT: you have misspelled your permission (you have STOREAGE, whereas the correct one is STORAGE)

Change

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

to:

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

Unless you're not doing it already in hasSDCard(), please check if your external media is available at all. As the docs say:

Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state.

An example they provide:

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;
}



回答2:


// try this
File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "MyApp" + "/");
if (!mediaStorageDir.exists()) {
      mediaStorageDir.mkdirs();
}

mediaStorageDir.getAbsolutePath()



回答3:


Try this:

mediaStorageDir = new File(
            Environment.getExternalStorageDirectory().getAbsolutePath(),
            "MyApp");



回答4:


This is what i am doing and its working for me

File path = Environment.getExternalStorageDirectory();
String save_path = path.toString() + "/MyApp/";
File newFile=new File(save_path );
newFile.mkdirs();


来源:https://stackoverflow.com/questions/20114206/android-failed-to-mkdir-on-sd-card

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