第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何為應(yīng)用程序創(chuàng)建簡(jiǎn)單的本地備份和恢復(fù)?

如何為應(yīng)用程序創(chuàng)建簡(jiǎn)單的本地備份和恢復(fù)?

哈士奇WWW 2023-08-04 15:46:42
我有五個(gè) sqlite 數(shù)據(jù)庫(kù),我希望用戶能夠在手機(jī)中進(jìn)行本地備份,并且可以恢復(fù)備份文件。我不知道如何創(chuàng)建這些備份并以編程方式恢復(fù)它們。我使用了 github 存儲(chǔ)庫(kù),但它根本不起作用,我需要你的幫助來(lái)創(chuàng)建這個(gè)備份和恢復(fù)過(guò)程。感謝您的關(guān)注
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊

在您的活動(dòng)中制作備份和恢復(fù)按鈕并定義本地?cái)?shù)據(jù)庫(kù)變量,例如,


private MainDatabase localBackup = new MainDatabase(this);

然后點(diǎn)擊即可進(jìn)行備份和恢復(fù)操作


@Override

public void onClick(View v) {

    final MainDatabase db = new MainDatabase(getApplicationContext());


    switch (v.getId()) {

        case R.id.tvBackUp:

            String outFileName = Environment.getExternalStorageDirectory() + 

File.separator + getResources().getString(R.string.app_name) + File.separator;

            localBackup.performBackup(db, outFileName);

            break;

        case R.id.tvRestore:

            File folder = new File(Environment.getExternalStorageDirectory() + File.separator + getApplicationContext().getResources().getString(R.string.app_name));

            if (folder.exists()) {


                final File[] files = folder.listFiles();


                if (files.length == 0) {

                    Toast.makeText(this, "No any Backup", Toast.LENGTH_SHORT).show();

                } else {

                    localBackup.performRestore(db);

                }

            }


            break;

    }

}

在數(shù)據(jù)庫(kù)文件中制定備份方法


public void performBackup(final MainDatabase db, final String outFileName) {


    File folder = new File(Environment.getExternalStorageDirectory() + File.separator 

+ mContext.getResources().getString(R.string.app_name));


    boolean success = true;

    if (!folder.exists())

        success = folder.mkdirs();

    if (success) {


        final Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.backup_dialog);

        dialog.getWindow().getAttributes().windowAnimations = 

R.style.PauseDialogAnimation;


dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        dialog.show();


        Button btnSave = dialog.findViewById(R.id.btnSave);

        Button btnCancel = dialog.findViewById(R.id.btnCancel);

        EditText etName = dialog.findViewById(R.id.etName);

        etName.setInputType(InputType.TYPE_CLASS_TEXT);

        btnSave.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String m_Text = etName.getText().toString();

                String out = outFileName + m_Text + ".db";


                db.backup(out);

                dialog.dismiss();

            }

        });

        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                dialog.dismiss();

            }

        });

    } else

        Toast.makeText(mContext, "Unable to create directory. Retry", 

Toast.LENGTH_SHORT).show();

}


    public void backup(String outFileName) {


    //database path

    final String inFileName = mContext.getDatabasePath(DATABASE_NAME).toString();


    try {

        File dbFile = new File(inFileName);

        FileInputStream fis = new FileInputStream(dbFile);


        // Open the empty db as the output stream

        OutputStream output = new FileOutputStream(outFileName);


        // Transfer bytes from the input file to the output file

        byte[] buffer = new byte[1024];

        int length;

        while ((length = fis.read(buffer)) > 0) {

            output.write(buffer, 0, length);

        }


        // Close the streams

        output.flush();

        output.close();

        fis.close();


        Toast.makeText(mContext, "Backup Completed", Toast.LENGTH_SHORT).show();


    } catch (Exception e) {

        Toast.makeText(mContext, "Unable to backup database. Retry", 

Toast.LENGTH_SHORT).show();

        e.printStackTrace();

    }

}

以及進(jìn)行恢復(fù)時(shí),詢問(wèn)用戶要恢復(fù)什么備份


public void performRestore(final MainDatabase db) {



    File folder = new File(Environment.getExternalStorageDirectory() + File.separator 

+ mContext.getResources().getString(R.string.app_name));

    if (folder.exists()) {


        final File[] files = folder.listFiles();


        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext, 

 android.R.layout.select_dialog_item);

        for (File file : files)

            arrayAdapter.add(file.getName());

        AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);


        builderSingle.setTitle("Select & Restore ");

        builderSingle.setNegativeButton("cancle", (dialog, which) -> 

dialog.dismiss());

        builderSingle.setAdapter(arrayAdapter, (dialog, which) -> {

            try {

                db.importDB(files[which].getPath());

            } catch (Exception e) {

                Toast.makeText(mContext, "Unable to restore. Retry", 

Toast.LENGTH_SHORT).show();

            }

        });


        builderSingle.show();

    } else

        Toast.makeText(mContext, "Backup folder not present.\nDo a backup before a 

restore!", Toast.LENGTH_SHORT).show();

}


 public void importDB(String inFileName) {


    final String outFileName = mContext.getDatabasePath(DATABASE_NAME).toString();


    try {


        File dbFile = new File(inFileName);


        FileInputStream fis = new FileInputStream(dbFile);


        // Open the empty db as the output stream

        OutputStream output = new FileOutputStream(outFileName);


        // Transfer bytes from the input file to the output file

        byte[] buffer = new byte[1024];

        int length;

        while ((length = fis.read(buffer)) > 0) {

            output.write(buffer, 0, length);

        }


        // Close the streams

        output.flush();

        output.close();

        fis.close();


        Toast.makeText(mContext, "Restore Completed", Toast.LENGTH_SHORT).show();


    } catch (Exception e) {

        Toast.makeText(mContext, "Unable to import database. Retry", 

Toast.LENGTH_SHORT).show();

        e.printStackTrace();

    }

}



查看完整回答
反對(duì) 回復(fù) 2023-08-04
?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

如果您的清單中有的話,Android 已經(jīng)支持系統(tǒng)自動(dòng)備份android:allowBackup="true"。如果這還不夠,并且您想在應(yīng)用程序重新安裝之間手動(dòng)管理備份,那么您必須將數(shù)據(jù)庫(kù)從context.getDatabasePath("<your-database-name>")外部存儲(chǔ)復(fù)制到某個(gè)地方,然后在需要時(shí)將其復(fù)制回來(lái)



查看完整回答
反對(duì) 回復(fù) 2023-08-04
  • 2 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)