我希望你在运行电话直接,手机将不允许访问数据文件夹。
- 创建自己的文件夹
- 数据库复制到位置
- 打开DB从该位置
对于参考
public void createDataBase() throws IOException {
String path=mContext.getDir("Folder_Name",
Context.MODE_WORLD_WRITEABLE).getAbsolutePath();
DB_PATH=path;
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist) {
try { copyDataBase(); }
catch (IOException mIOException)
{ Log.d("Exception",mIOException.getMessage());
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase() {
Log.d(TAG, "In checkDataBase :::::");
File dbFile = new File(DB_PATH+DATABASE_NAME);
Log.d("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists(); }
//Copy the database from assets
private void copyDataBase() throws IOException {
Log.d(TAG, "In copyDataBase :::::");
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
Log.d(TAG, "In copyDataBase outFileName:::::"+outFileName);
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();
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException {
//DB_PATH + String mPath = DB_PATH+ DATABASE_NAME;
Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
我已经纠正缺少斜杠,但我仍然有同样的错误 – 2011-05-29 14:18:55
什么是例外?哪条线? – redline 2011-05-29 19:30:19