我认为我已经做好了一切准备,首先创建一个文件夹,以便稍后在我的应用程序中进行写入,但似乎并没有起作用。有什么线索能告诉我我失败了吗?
它正确地尝试创建 /mnt/sdcard/gradebook
我在清单中使用了uses-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");
}
这是清单文件。
<?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>