无法在/ mnt/sdcard中创建文件夹

Mar*_*tin 4 android

我想我正在做一切正确的事情,首先创建一个文件夹,我可以稍后在我的应用程序中写入,但它似乎没有工作.关于我如何失败的任何线索?

它正确地尝试创建/ mnt/sdcard/gradebook

我的清单中有use-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE".我正在运行一个真实设备(三星Galaxy S),而且我没有使用SD卡作为存储,而它连接到我的电脑.

    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;
        Toast.makeText(this,"This Application needs a writable external storage (sdcard).",Toast.LENGTH_SHORT).show();
        finish();
    } 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;
        Toast.makeText(this,"This Application needs a mounted external storage (sdcard).",Toast.LENGTH_SHORT).show();
        finish();

    }


    File folder = new File(Environment.getExternalStorageDirectory () + "/gradeBook");
    boolean success = false;
    if(!folder.exists()){
         success = folder.mkdir();
    }
    if (!success) {
        Log.e("FILE", "can't create " + folder);
    }
    else 
    {
        Log.i("FILE", "directory is created"); 
    }
Run Code Online (Sandbox Code Playgroud)

这是清单

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ulsanonline.gradebook" 
    android:versionName="@string/version" 
    android:installLocation="auto" 
    android:versionCode="2">
    <uses-sdk android:minSdkVersion="8"></uses-sdk> 
    <application 
        android:icon="@drawable/icon" 
        android:debuggable="true" 
        android:persistent="false" 
        android:hasCode="true"
        android:minSdkVersion="8">
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.CAMERA" />
        <activity 
            android:name=".CourseWork" 
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Run Code Online (Sandbox Code Playgroud)

ayu*_*lin 8

uses-permission必须直接作为<manifest>孩子放置,所以它应该是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ulsanonline.gradebook" 
    android:versionName="@string/version" 
    android:installLocation="auto" 
    android:versionCode="2">
    <uses-sdk android:minSdkVersion="8"></uses-sdk> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <application 
        android:icon="@drawable/icon" 
        android:debuggable="true" 
        android:persistent="false"
Run Code Online (Sandbox Code Playgroud)