在android中创建目录

时间:2013-01-21 12:48:07

标签: android

android中有一些限制你可以一次创建多少个目录? 因为我实际上是想在子子目录中保存我的数据库。所以我使用以下代码..

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

当我第一次创建名为“AllDatabases”的文件夹时,代码工作正常。然后是一个子文件夹,其中包含变量'comp'的某个特定值(比如'lol')

 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

请建议我一些替代方案或告诉我如何避免此限制。

感谢。 :)

1 个答案:

答案 0 :(得分:0)

您需要逐个创建子文件夹,而不是同时创建子文件夹。 例如。如果您有一个包含子文件夹名称的数组文件夹,那么

File cnxDir = Environment.getExternalStorageDirectory();

    for (String folder : folders) {

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

        }

    }