当我尝试使用该方法打开资产文件夹中的数据库时
public void loadDatabase(String dbName){
System.out.println("dbName");
DataBaseAdapterNew mDbHelper = new DataBaseAdapterNew(
this, dbName);
mDbHelper.createDatabase();
mDbHelper.open();
}
我得到一个致命的异常ErrorCopyingDataBase
错误堆栈
12-13 18:21:29.179: E/AndroidRuntime(10230): FATAL EXCEPTION: main
12-13 18:21:29.179: E/AndroidRuntime(10230): java.lang.Error: ErrorCopyingDataBase
12-13 18:21:29.179: E/AndroidRuntime(10230): at com.my.app.db.DataBaseHelper.createDataBase(DataBaseHelper.java:43)
12-13 18:21:29.179: E/AndroidRuntime(10230): at com.my.app.db.DataBaseAdapterNew.createDatabase(DataBaseAdapterNew.java:42)
12-13 18:21:29.179: E/AndroidRuntime(10230): at com.my.app.MyActivity.loadDatabase(MyActivity.java:133)
12-13 18:21:29.179: E/AndroidRuntime(10230): at com.my.app.MyActivity$InvocazioneAsincrona$1.run(MyActivity.java:173)
12-13 18:21:29.179: E/AndroidRuntime(10230): at android.os.Handler.handleCallback(Handler.java:587)
12-13 18:21:29.179: E/AndroidRuntime(10230): at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 18:21:29.179: E/AndroidRuntime(10230): at android.os.Looper.loop(Looper.java:130)
12-13 18:21:29.179: E/AndroidRuntime(10230): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-13 18:21:29.179: E/AndroidRuntime(10230): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 18:21:29.179: E/AndroidRuntime(10230): at java.lang.reflect.Method.invoke(Method.java:507)
12-13 18:21:29.179: E/AndroidRuntime(10230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-13 18:21:29.179: E/AndroidRuntime(10230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-13 18:21:29.179: E/AndroidRuntime(10230): at dalvik.system.NativeStart.main(Native Method)
我的 DatabaseAdapterNew 类是:
public class DataBaseAdapterNew {
protected static final String TAG = "DataAdapter";
private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;
public DataBaseAdapterNew(Context context, String nameDB) {
this.mContext = context;
mDbHelper = new DataBaseHelper(mContext, nameDB);
}
public DataBaseAdapterNew createDatabase() throws SQLException {
try {
mDbHelper.createDataBase();
} catch (IOException mIOException) {
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public DataBaseAdapterNew open() throws SQLException {
try {
mDbHelper.openDataBase();
mDbHelper.close();
mDb = mDbHelper.getReadableDatabase();
} catch (SQLException mSQLException) {
Log.e(TAG, "open >>" + mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close() {
mDbHelper.close();
}
public Cursor allData() {
try {
Cursor c = mDb.rawQuery(
"SELECT * FROM myTable order by Quantity desc", null);
if (c != null) {
c.moveToNext();
}
return c;
} catch (SQLException mSQLException) {
Log.e(TAG, "allData >>" + mSQLException.toString());
throw mSQLException;
}
}
}
我的 DatabaseHelper 类是:
public class DataBaseHelper extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper";
private static String DB_PATH = "";
private static String DB_NAME = "";
private SQLiteDatabase mDataBase;
private final Context mContext;
public DataBaseHelper(Context context, String nomeDB) {
super(context, nomeDB, null, 1);// 1? its Database Version
DB_NAME = nomeDB;
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException {
// If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if (!mDataBaseExist) {
this.getReadableDatabase();
this.close();
try {
// Copia il database dalla cartella assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
} catch (IOException mIOException) {
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException {
String mPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(mPath, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
传递的 String dbName显示了 println 中assets文件夹中存在的 db 的正确名称,并且数据库不是 null 并且是预编译的,所以我不知道错误的原因是什么。有什么建议吗?