1

这是我的代码:

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
4

2 回答 2

5

我不确定是否只是尝试创建如下文件夹 -

success = myFolder.mkdirs();

代替

success = myFolder.mkdir();

根据mkdirs()并且,不要忘记将以下权限添加到您的清单文件中。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

也看看这个答案 -如何在 SD 卡上自动创建目录

于 2012-09-26T15:27:38.830 回答
0

您应该在 mainfest 文件中使用此权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-09-26T15:24:36.863 回答