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

标签 android

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

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

我有使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE" 在我的 list 中。我在真实设备 (Samsung 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"); 
    }

这是 list

<?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>

最佳答案

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

<?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"

关于android - 无法在/mnt/sdcard 中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6921018/

相关文章:

android - 可重用布局android的类

android - 如何将Edittext中的文字和笑脸转为字符串?

android - 如何强制 RadioGroup 中的 ToggleButton 在第二次按下时保持选中状态?

android - 什么是 applicationVariant.outputs?

android - 播放 mp3/wav 和改变速度(音频速度)Android 的更好方法是什么?

java - 处理具有空对象响应的 Web 服务

java - 当我使用 Ant 构建具有多个库的 Android 项目时,我的构建失败

Android 模拟/发送 ACTION_MEDIA_BUTTON Intent

Android PopupWindow 不关闭

android - 状态栏下方的抽屉导航