我认为我正在做一切正确的事情来创建一个文件夹 我可以在我的应用程序中稍后写,但它似乎没有工作。 关于我如何失败的任何线索?
它正确地尝试创建/ mnt / sdcard / gradebook
我有use-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE” 在我的清单中。我正在运行一个真正的设备(三星Galaxy S),我没有使用 sdcard作为存储,当它连接到我的电脑。
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>
答案 0 :(得分: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"
答案 1 :(得分:0)
试试这个,
//Declare a string
String NewFolder = "/myNewFolder";
String extStorageDirectory;
//external storage directory path
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
//creating a new folder
File mkTarDir=new File(extStorageDirectory + NewFolder);
mkTarDir.mkdir();
//path to save a image
File file = new File(extStorageDirectory + NewFolder ,"myimage"+String.valueOf(System.currentTimeMillis()) + ".PNG");