以下是导入和导出SQLite数据库的工作方法。在除Android Pie外的所有android版本中,其工作情况都很好。当我尝试导入Android Pie时,它显示成功的Toast,但未还原数据库。谁能帮我解决Android Pie(API 28)问题。private void importDB() { try { File sd = Environment.getExternalStorageDirectory(); File cur_db_pat = new File(this.getDatabasePath(DATABASE_NAME).getAbsolutePath()); if (sd.canWrite()) { String backupDBPath = bac_dir_nam +"/" + DATABASE_NAME; File currentDB = new File(sd, backupDBPath); FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(cur_db_pat).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(getBaseContext(), cur_db_pat.toString(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) .show(); }}private void exportDB() { try { File sd = Environment.getExternalStorageDirectory(); File cur_db_pat = new File(this.getDatabasePath(DATABASE_NAME).getAbsolutePath()); if (sd.canWrite()) { String backupDBPath = bac_dir_nam+"/" + DATABASE_NAME; File backupDB = new File(sd, backupDBPath); FileChannel src = new FileInputStream(cur_db_pat).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) .show(); }}我对文件系统没有太多经验。因此,举个例子会很有帮助。
2 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
在您的Db WorkHelper ovverride onOpen()方法类中,设置disableWriteAheadLogging,然后调用onOpen()标准,如果android sdk 28版本,则确保旧版本仍然是旧模式。
@Override
public void onOpen(SQLiteDatabase database) {
super.onOpen(database);
if(Build.VERSION.SDK_INT >= 28)
{
database.disableWriteAheadLogging();
}
}
就我而言,工作完美。
添加回答
举报
0/150
提交
取消