Android无法在SD卡上使用mkdir()

All*_*ang 1 java storage android file android-file

我有一些代码在SD卡上创建/获取文件夹:

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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

并且我在我的清单文件中注册了许可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STOREAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,会打印:

Create image directory FAILED. path: /storage/emulated/0/Pictures/MyApp
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会这样?

===更新===

我尝试了所有这些:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

但他们都表现得完全一样......

===更新2 ===

我正在使用Nexus 5进行测试.我看到这篇文章在Eclipse中找不到存储/模拟/ 0/Nexus 7文件夹 可能存在一些问题?

===更新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
Run Code Online (Sandbox Code Playgroud)

===更新4 ===

这是我的hasSD()方法:

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

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

    return fHasSDCard;
}
Run Code Online (Sandbox Code Playgroud)

Mel*_*des 9

编辑:你拼错了你的许可(你有STOREAGE,而正确的是STORAGE)

更改

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STOREAGE"/>
Run Code Online (Sandbox Code Playgroud)

至:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)

除非你在hasSDCard()中没有这样做,否则请检查你的外部媒体是否可用.正如文档所说:

在使用外部存储器进行任何操作之前,应始终调用getExternalStorageState()来检查介质是否可用.媒体可能安装在计算机上,丢失,只读或处于其他状态.

他们提供的一个例子:

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;
}
Run Code Online (Sandbox Code Playgroud)