以下是導入和導出SQLite數(shù)據(jù)庫的工作方法。在除Android Pie外的所有android版本中,其工作情況都很好。當我嘗試導入Android Pie時,它顯示成功的Toast,但未還原數(shù)據(jù)庫。誰能幫我解決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(); }}我對文件系統(tǒng)沒有太多經(jīng)驗。因此,舉個例子會很有幫助。
2 回答

撒科打諢
TA貢獻1934條經(jīng)驗 獲得超2個贊
在您的Db WorkHelper ovverride onOpen()方法類中,設置disableWriteAheadLogging,然后調(diào)用onOpen()標準,如果android sdk 28版本,則確保舊版本仍然是舊模式。
@Override
public void onOpen(SQLiteDatabase database) {
super.onOpen(database);
if(Build.VERSION.SDK_INT >= 28)
{
database.disableWriteAheadLogging();
}
}
就我而言,工作完美。
添加回答
舉報
0/150
提交
取消