在我的一个 Android 应用程序中,我使用了一个预填充的数据库,我会在第一次使用时复制它。到目前为止,这在所有 Android 版本上运行良好,但对于 API 28,它会失败。它确实复制了数据库,之后它可以连接到数据库,但由于某种原因,无法访问任何表。我得到该表不存在的错误。
我在模拟器上下载了数据库,检查了一下,数据库没有问题。我在代码中创建数据库的另一个应用程序工作正常(创建数据库,然后可以找到表)。
我用来复制数据库的代码:
public void copyDatabase() throws IOException{
InputStream myInput = mContext.getAssets().open(mDbSource);
// Path to the just created empty db
String outFileName = getDbPath() + mDbName;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private String getDbPath(){
return "/data/data/"+mContext.getResources().getString(R.string.package_name)+"/databases/";
}
API 28 中是否有任何更改导致此操作因某种原因失败?这可能是什么原因造成的?
德玛西亚99
aluckdog
慕少森
catspeake
随时随地看视频慕课网APP
相关分类