如何为应用程序创建简单的本地备份和恢复?

我有五个 sqlite 数据库,我希望用户能够在手机中进行本地备份,并且可以恢复备份文件。我不知道如何创建这些备份并以编程方式恢复它们。我使用了 github 存储库,但它根本不起作用,我需要你的帮助来创建这个备份和恢复过程。感谢您的关注



哈士奇WWW
浏览 114回答 2
2回答

慕桂英4014372

在您的活动中制作备份和恢复按钮并定义本地数据库变量,例如,private MainDatabase localBackup = new MainDatabase(this);然后点击即可进行备份和恢复操作@Overridepublic void onClick(View v) {&nbsp; &nbsp; final MainDatabase db = new MainDatabase(getApplicationContext());&nbsp; &nbsp; switch (v.getId()) {&nbsp; &nbsp; &nbsp; &nbsp; case R.id.tvBackUp:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String outFileName = Environment.getExternalStorageDirectory() +&nbsp;File.separator + getResources().getString(R.string.app_name) + File.separator;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localBackup.performBackup(db, outFileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case R.id.tvRestore:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File folder = new File(Environment.getExternalStorageDirectory() + File.separator + getApplicationContext().getResources().getString(R.string.app_name));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (folder.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final File[] files = folder.listFiles();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (files.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "No any Backup", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localBackup.performRestore(db);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}在数据库文件中制定备份方法public void performBackup(final MainDatabase db, final String outFileName) {&nbsp; &nbsp; File folder = new File(Environment.getExternalStorageDirectory() + File.separator&nbsp;+ mContext.getResources().getString(R.string.app_name));&nbsp; &nbsp; boolean success = true;&nbsp; &nbsp; if (!folder.exists())&nbsp; &nbsp; &nbsp; &nbsp; success = folder.mkdirs();&nbsp; &nbsp; if (success) {&nbsp; &nbsp; &nbsp; &nbsp; final Dialog dialog = new Dialog(mContext);&nbsp; &nbsp; &nbsp; &nbsp; dialog.setContentView(R.layout.backup_dialog);&nbsp; &nbsp; &nbsp; &nbsp; dialog.getWindow().getAttributes().windowAnimations =&nbsp;R.style.PauseDialogAnimation;dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);&nbsp; &nbsp; &nbsp; &nbsp; dialog.show();&nbsp; &nbsp; &nbsp; &nbsp; Button btnSave = dialog.findViewById(R.id.btnSave);&nbsp; &nbsp; &nbsp; &nbsp; Button btnCancel = dialog.findViewById(R.id.btnCancel);&nbsp; &nbsp; &nbsp; &nbsp; EditText etName = dialog.findViewById(R.id.etName);&nbsp; &nbsp; &nbsp; &nbsp; etName.setInputType(InputType.TYPE_CLASS_TEXT);&nbsp; &nbsp; &nbsp; &nbsp; btnSave.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String m_Text = etName.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String out = outFileName + m_Text + ".db";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.backup(out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; btnCancel.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Unable to create directory. Retry",&nbsp;Toast.LENGTH_SHORT).show();}&nbsp; &nbsp; public void backup(String outFileName) {&nbsp; &nbsp; //database path&nbsp; &nbsp; final String inFileName = mContext.getDatabasePath(DATABASE_NAME).toString();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; File dbFile = new File(inFileName);&nbsp; &nbsp; &nbsp; &nbsp; FileInputStream fis = new FileInputStream(dbFile);&nbsp; &nbsp; &nbsp; &nbsp; // Open the empty db as the output stream&nbsp; &nbsp; &nbsp; &nbsp; OutputStream output = new FileOutputStream(outFileName);&nbsp; &nbsp; &nbsp; &nbsp; // Transfer bytes from the input file to the output file&nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[1024];&nbsp; &nbsp; &nbsp; &nbsp; int length;&nbsp; &nbsp; &nbsp; &nbsp; while ((length = fis.read(buffer)) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.write(buffer, 0, length);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Close the streams&nbsp; &nbsp; &nbsp; &nbsp; output.flush();&nbsp; &nbsp; &nbsp; &nbsp; output.close();&nbsp; &nbsp; &nbsp; &nbsp; fis.close();&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Backup Completed", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Unable to backup database. Retry",&nbsp;Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}以及进行恢复时,询问用户要恢复什么备份public void performRestore(final MainDatabase db) {&nbsp; &nbsp; File folder = new File(Environment.getExternalStorageDirectory() + File.separator&nbsp;+ mContext.getResources().getString(R.string.app_name));&nbsp; &nbsp; if (folder.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; final File[] files = folder.listFiles();&nbsp; &nbsp; &nbsp; &nbsp; final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext,&nbsp;&nbsp;android.R.layout.select_dialog_item);&nbsp; &nbsp; &nbsp; &nbsp; for (File file : files)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrayAdapter.add(file.getName());&nbsp; &nbsp; &nbsp; &nbsp; AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);&nbsp; &nbsp; &nbsp; &nbsp; builderSingle.setTitle("Select & Restore ");&nbsp; &nbsp; &nbsp; &nbsp; builderSingle.setNegativeButton("cancle", (dialog, which) ->&nbsp;dialog.dismiss());&nbsp; &nbsp; &nbsp; &nbsp; builderSingle.setAdapter(arrayAdapter, (dialog, which) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.importDB(files[which].getPath());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Unable to restore. Retry",&nbsp;Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; builderSingle.show();&nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Backup folder not present.\nDo a backup before a&nbsp;restore!", Toast.LENGTH_SHORT).show();}&nbsp;public void importDB(String inFileName) {&nbsp; &nbsp; final String outFileName = mContext.getDatabasePath(DATABASE_NAME).toString();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; File dbFile = new File(inFileName);&nbsp; &nbsp; &nbsp; &nbsp; FileInputStream fis = new FileInputStream(dbFile);&nbsp; &nbsp; &nbsp; &nbsp; // Open the empty db as the output stream&nbsp; &nbsp; &nbsp; &nbsp; OutputStream output = new FileOutputStream(outFileName);&nbsp; &nbsp; &nbsp; &nbsp; // Transfer bytes from the input file to the output file&nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[1024];&nbsp; &nbsp; &nbsp; &nbsp; int length;&nbsp; &nbsp; &nbsp; &nbsp; while ((length = fis.read(buffer)) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.write(buffer, 0, length);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Close the streams&nbsp; &nbsp; &nbsp; &nbsp; output.flush();&nbsp; &nbsp; &nbsp; &nbsp; output.close();&nbsp; &nbsp; &nbsp; &nbsp; fis.close();&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Restore Completed", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "Unable to import database. Retry",&nbsp;Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}

拉莫斯之舞

如果您的清单中有的话,Android 已经支持系统自动备份android:allowBackup="true"。如果这还不够,并且您想在应用程序重新安装之间手动管理备份,那么您必须将数据库从context.getDatabasePath("<your-database-name>")外部存储复制到某个地方,然后在需要时将其复制回来
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java