greenDAO を使用して sdcard にデータベースを作成しようとしていますが、例外が発生します。ここに、DatabaseManager.java および DAOMaster.java ファイルのスニペット コードを添付しました。ご覧になり、ご提案をお願いします。ありがとうございました。:)
DatabaseManager.java
public class DatabaseManager implements IDatabaseManager,AsyncOperationListener {
/**
* Class tag. Used for debug.
*/
private static final String TAG = DatabaseManager.class.getCanonicalName();
/**
* Instance of DatabaseManager
*/
private static DatabaseManager instance;
/**
* The Android Activity reference for access to DatabaseManager.
*/
private Context context;
private DaoMaster.DevOpenHelper mHelper;
private SQLiteDatabase database;
private DaoMaster daoMaster;
private DaoSession daoSession;
private AsyncSession asyncSession;
private List<AsyncOperation> completedOperations;
/**
* Constructs a new DatabaseManager with the specified arguments.
*
* @param context The Android {@link android.content.Context}.
*/
public DatabaseManager(final Context context) {
File path = new File(Environment.getExternalStorageDirectory(), "Android/MobInvSuit/mis");
path.getParentFile().mkdirs();
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this.context, path.getAbsolutePath(), null);
database=helper.getWritableDatabase();
daoMaster=new DaoMaster(database);
// this.context = context;
// mHelper = new DaoMaster.DevOpenHelper(this.context, "sample-database", null);
completedOperations = new CopyOnWriteArrayList<AsyncOperation>();
}
/**
* @param context The Android {@link android.content.Context}.
* @return this.instance
*/
public static DatabaseManager getInstance(Context context) {
if (instance == null) {
instance = new DatabaseManager(context);
}
return instance;
}
@Override
public void onAsyncOperationCompleted(AsyncOperation operation) {
completedOperations.add(operation);
}
private void assertWaitForCompletion1Sec() {
asyncSession.waitForCompletion(1000);
asyncSession.isCompleted();
}
/**
* Query for readable DB
*/
public void openReadableDb() throws SQLiteException {
database = mHelper.getReadableDatabase();
daoMaster = new DaoMaster(database);
daoSession = daoMaster.newSession();
asyncSession = daoSession.startAsyncSession();
asyncSession.setListener(this);
}
/**
* Query for writable DB
*/
public void openWritableDb() throws SQLiteException {
database = mHelper.getWritableDatabase();
daoMaster = new DaoMaster(database);
daoSession = daoMaster.newSession();
asyncSession = daoSession.startAsyncSession();
asyncSession.setListener(this);
}
@Override
public void closeDbConnections() {
if (daoSession != null) {
daoSession.clear();
daoSession = null;
}
if (database != null && database.isOpen()) {
database.close();
}
if (mHelper != null) {
mHelper.close();
mHelper = null;
}
if (instance != null) {
instance = null;
}
}
@Override
public synchronized void dropDatabase() {
try {
openWritableDb();
DaoMaster.dropAllTables(database, true); // drops all tables
mHelper.onCreate(database); // creates the tables
asyncSession.deleteAll(Setting.class); // clear all elements from a table
} catch (Exception e) {
e.printStackTrace();
}
}
}
DAOMaster.java
package com.example.cisystem3.mobiledeliveryndispatch.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import de.greenrobot.dao.AbstractDaoMaster;
import de.greenrobot.dao.identityscope.IdentityScopeType;
import com.example.cisystem3.mobiledeliveryndispatch.dao.SettingDao;
import com.example.cisystem3.mobiledeliveryndispatch.dao.UserDao;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* Master of DAO (schema version 1): knows all DAOs.
*/
public class DaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 1;
/** Creates underlying database table using DAOs. */
public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) {
SettingDao.createTable(db, ifNotExists);
UserDao.createTable(db, ifNotExists);
}
/** Drops underlying database table using DAOs. */
public static void dropAllTables(SQLiteDatabase db, boolean ifExists) {
SettingDao.dropTable(db, ifExists);
UserDao.dropTable(db, ifExists);
}
public static abstract class OpenHelper extends SQLiteOpenHelper {
public OpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
createAllTables(db, false);
}
}
/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}
public DaoMaster(SQLiteDatabase db) {
super(db, SCHEMA_VERSION);
registerDaoClass(SettingDao.class);
registerDaoClass(UserDao.class);
}
public DaoSession newSession() {
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}
public DaoSession newSession(IdentityScopeType type) {
return new DaoSession(db, type, daoConfigMap);
}
}
例外
FATAL EXCEPTION: main
Process: com.example.cisystem3.mobiledeliveryndispatch, PID: 30820
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cisystem3.mobiledeliveryndispatch/com.example.cisystem3.mobiledeliveryndispatch.Login}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) at android.os.Handler.dispatchMessage(Handler.java:111)at android.os.Looper.loop(Looper.java:207 at android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open databaseat android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
マニフェスト
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cisystem3.mobiledeliveryndispatch">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>