这是我的代码:
boolean success = false;
Log.d(TAG, Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_PICTURES + "/myFolder" );
myFolder = new File( Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_PICTURES + "/myFolder" );
if( myFolder.exists() ){
Log.d(TAG, "FOLDER EXISTS");
}else{
success = myFolder.mkdir();
if( success ){
// Do something on success
} else {
// Do something else on failure
throw new RuntimeException("File Error in writing new folder");
}
有什么问题?
在HTC Desire HD - android 2.3.5
上运行会导致此输出:
D/DVA_HLUI Application(25723): /mnt/sdcard/Pictures/myFolder
E/AndroidRuntime(25723): FATAL EXCEPTION: main
E/AndroidRuntime(25723): java.lang.RuntimeException: Unable to create application com.DVA_HLUI.DVAHLUI_App: java.lang.RuntimeException: File Error in writing new folder
E/AndroidRuntime(25723): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3920)
E/AndroidRuntime(25723): at android.app.ActivityThread.access$2200(ActivityThread.java:135)
E/AndroidRuntime(25723): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1098)
E/AndroidRuntime(25723): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25723): at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(25723): at android.app.ActivityThread.main(ActivityThread.java:4385)
E/AndroidRuntime(25723): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25723): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(25723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
E/AndroidRuntime(25723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime(25723): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25723): Caused by: java.lang.RuntimeException: File Error in writing new folder
E/AndroidRuntime(25723): at com.DVA_HLUI.DVAHLUI_App.onCreate(DVAHLUI_App.java:185)
E/AndroidRuntime(25723): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:984)
E/AndroidRuntime(25723): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3917)
E/AndroidRuntime(25723): ... 10 more
答案 0 :(得分:5)
我不确定只是尝试创建如下文件夹 -
success = myFolder.mkdirs();
而不是
success = myFolder.mkdir();
根据mkdirs()并且,不要忘记将以下许可添加到您的清单文件中。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
您应该在清单文件中使用此权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>