2013-01-21 139 views
0

在android中有一些限制可以一次創建多少個目錄? 因爲我實際上試圖將我的數據庫保存在子子目錄下。所以,我用的是下面的代碼..在android中創建目錄

DATABASE_NAME = Environment.getExternalStorageDirectory() + "/AllDatabases/" + comp 
      + "/Balance/" + wy + ".db"; 

代碼工作絕對沒問題,當我第一次創建一個文件夾名爲「AllDatabases」 &然後在其中一個子文件夾的變量「補償」的一些特定值(說'大聲笑')

where comp = lol 

然後它成功地在那裏創建數據庫。但是當我再次刪除創建的文件夾&直接嘗試使用上述語句在我將DATABASE_NAME分配一些值的情況下構建它時,它會強制關閉我的應用程序。這是什麼LogCat顯示...

01-21 18:03:30.194: E/Database(19098): sqlite3_open_v2("/mnt/sdcard/lol/MyLedgers/2012-2013.db", &handle, 6, NULL) failed 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): Couldn't open /mnt/sdcard/lol/MyLedgers/2012-2013.db for writing (will try read-only): 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): android.database.sqlite.SQLiteException: unable to open database file 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1921) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:883) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:960) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:953) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:548) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at com.balance.start.LedgerInfoDB.opentoread(LedgerInfoDB.java:78) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at com.balance.start.MainView.onCreate(MainView.java:101) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.ActivityThread.access$2300(ActivityThread.java:126) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.os.Looper.loop(Looper.java:123) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at android.app.ActivityThread.main(ActivityThread.java:4633) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at java.lang.reflect.Method.invokeNative(Native Method) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at java.lang.reflect.Method.invoke(Method.java:521) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-21 18:03:30.254: E/SQLiteOpenHelper(19098): at dalvik.system.NativeStart.main(Native Method) 
01-21 18:03:30.254: E/Database(19098): sqlite3_open_v2("/mnt/sdcard/chomu/MyLedgers/2012-2013.db", &handle, 1, NULL) failed 
01-21 18:03:30.264: W/dalvikvm(19098): threadid=1: thread exiting with uncaught exception (group=0x400207d8) 
01-21 18:03:30.264: E/AndroidRuntime(19098): FATAL EXCEPTION: main 
01-21 18:03:30.264: E/AndroidRuntime(19098): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.balance.start/com.balance.start.MainView}: android.database.sqlite.SQLiteException: unable to open database file 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.ActivityThread.access$2300(ActivityThread.java:126) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.os.Looper.loop(Looper.java:123) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.ActivityThread.main(ActivityThread.java:4633) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at java.lang.reflect.Method.invokeNative(Native Method) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at java.lang.reflect.Method.invoke(Method.java:521) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at dalvik.system.NativeStart.main(Native Method) 
01-21 18:03:30.264: E/AndroidRuntime(19098): Caused by: android.database.sqlite.SQLiteException: unable to open database file 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1921) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:883) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:168) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at com.balance.start.LedgerInfoDB.opentoread(LedgerInfoDB.java:78) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at com.balance.start.MainView.onCreate(MainView.java:101) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-21 18:03:30.264: E/AndroidRuntime(19098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) 
01-21 18:03:30.264: E/AndroidRuntime(19098): ... 11 more 

請建議我一些替代或告訴我我怎樣才能避免這種限制。

謝謝。 :)

回答

0

您需要一個一個創建子文件夾,而不是同時創建。例如,如果您有一個包含子文件夾名稱的陣列文件夾,然後

File cnxDir = Environment.getExternalStorageDirectory(); 

    for (String folder : folders) { 

     cnxDir = new File(cnxDir.toString() + File.separator + folder); 
     if (!cnxDir.exists()) { 
      cnxDir.mkdir(); 

     } 

    }