亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從無根設備中的資產文件夾復制數據庫

從無根設備中的資產文件夾復制數據庫

慕娘9325324 2019-10-21 12:44:51
我正在嘗試將DB從Assets文件夾復制到設備。這段代碼在模擬器和根設備上運行正常。我只想知道它是在無根設備上創建任何問題還是可以正常工作。private void StoreDatabase() {    File DbFile = new File(            "data/data/packagename/DBname.sqlite");    if (DbFile.exists()) {        System.out.println("file already exist ,No need to Create");    } else {        try {            DbFile.createNewFile();            System.out.println("File Created successfully");            InputStream is = this.getAssets().open("DBname.sqlite");            FileOutputStream fos = new FileOutputStream(DbFile);            byte[] buffer = new byte[1024];            int length = 0;            while ((length = is.read(buffer)) > 0) {                fos.write(buffer, 0, length);            }            System.out.println("File succesfully placed on sdcard");            // Close the streams            fos.flush();            fos.close();            is.close();        } catch (IOException e) {            e.printStackTrace();        }    }}
查看完整描述

3 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

我不確定,但這可以在我測試過的每臺設備上使用。我從這里的某個地方偷了這個方法,并使其在備份和還原方面通用:


public static void movedb(File srcdb, File destdb)

{

    try 

    {

        if (Environment.getExternalStorageDirectory().canWrite()) 

        {                 

            if (srcdb.exists()) 

            {

                FileChannel src = new FileInputStream(srcdb).getChannel();

                FileChannel dst = new FileOutputStream(destdb).getChannel();

                dst.transferFrom(src, 0, src.size());

                src.close();

                dst.close();                    

            }

            else

            {

                //ERROR: "Database file references are incorrect"                    

            }

        }

        else

        {

           //ERROR: "Cannot write to file"

        }

    }

    catch (Exception e) 

    {

        //ERROR: e.getMessage()

    }

}

然后我通過調用以下內容進行備份:


movedb(this, getDatabasePath(getDbName()), new File(Environment.getExternalStorageDirectory(), getDatabaseBackupPath()));

哪里getDatabasePath()和getDatabaseBackupPath()只是字符串值


查看完整回答
反對 回復 2019-10-21
  • 3 回答
  • 0 關注
  • 344 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號