Android-从资产复制数据库
以下是我在许多答案中使用的代码:Android-从资产复制数据库,android,database,sqlite,fileoutputstream,Android,Database,Sqlite,Fileoutputstream,以下是我在许多答案中使用的代码: InputStream myInput; try { myInput = iNezamApplication.getAppContext().getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFi
InputStream myInput;
try {
myInput = iNezamApplication.getAppContext().getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
但是,在到达OutStream行后,我总是会遇到一个异常:
java.io.FileNotFoundException: /data/data/package_name/databases/databasename.db: open failed: ENOENT (No such file or directory)
package_name是否只是为了在此处发布而替代您的真实package name,还是在您的DB_路径中真正使用此名称 首先必须创建一个文件对象我尝试了类似的方法
final String[] sample_dbName = {"DrugsNew.db"};
int assetDbSize = sample_dbName.length;
File databaseFile = new File( "/data/data/com.handyrep2.ui/databases");
// check if databases folder exists, if not create one and its subfolders
if (!databaseFile.exists()){
databaseFile.mkdir();
}
for(int i=0;i<assetDbSize;i++){
String outFilename =null;
outFilename = "/data/data/com.handyrep2.ui/databases"+ "/" + sample_dbName[i];
File sampleFile = new File(outFilename);
try {
InputStream in = activity.getAssets().open("offlinedb/"+sample_dbName[i]);
OutputStream out = new FileOutputStream(outFilename);
// Transfer bytes from the sample input file to the sample output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
// Close the streams
out.close();
in.close();
}catch(IOException e) {
}
}
请考虑切换到测试、调试和支持的SqListaseSelpter,而不是在许多答案中找到的老化、不支持的COD E:谢谢,我知道,但我不想使用它。我只需要回答我的问题。不,这是真实的包裹名称,我只是省略了:好的。我不知道。我们在当前的项目中使用了几乎相同的方法——效果很好,所以到目前为止我没有看到任何错误。抱歉:需要先创建数据库目录。谢谢: