我有一个名为MainExerciseDatabaseOne.db的sqlite数据库文件存储在我的app/src/main/assets/databases文件夹中。我想在我的主要活动的功能,打开文件:无法在android中加载数据库文件
private ExerciseDb createDatabase(){
final String inFileName = "/data/data/<mypackage>/databases/MainExerciseDatabaseOne.db";
File dbFile = new File(inFileName);
FileInputStream fis = null;
try {
fis = new FileInputStream(dbFile);
} catch (FileNotFoundException e) {
Log.e("error",e.toString());
}
String outFileName = DB_PATH + ExerciseDb.DATABASE_NAME;
// Open the empty db as the output stream
OutputStream output = null;
try {
output = new FileOutputStream(outFileName);
} catch (FileNotFoundException e) {
Log.e("error",e.toString());
}
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
try {
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
} catch (IOException e) {
Log.e("error",e.toString());
}
// Close the streams
try {
output.flush();
output.close();
fis.close();
} catch (IOException e) {
Log.e("error",e.toString());
}
ExerciseDb ret = new ExerciseDb(this);
return ret;
}
,但我得到未发现异常就行了文件:
fis = new FileInputStream(dbFile);
在每一个教程中,我看这似乎是访问这个文件的正确路径,所以我不确定它为什么不能正常工作。这也是奇怪,因为这个确切的代码没有工作,直到我卸载了我的手机上的应用程序,并做了一个干净的构建,并从Android Studio中重新安装应用程序
只需使用[SQLiteAssetHelper(http://jgilfelt.github.io/android-sqlite-asset-helper/),而不是 –