备份/恢复应用程序数据库和Android的设置 [英] Backup/Restore application database and setting on Android

查看:143
本文介绍了备份/恢复应用程序数据库和Android的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充的能力,为用户备份应用程序数据库,并设置到SD卡。那么应该能够恢复在其它设备上的所有数据。

I want to add ability for users to backup application database and settings to SD card. Then should be able to restore all data on another device.

如何通常这些任务都做了什么?我需要recomendations /样品。

How usually these tasks are done? I need recomendations/samples.

推荐答案

通过数据库你基本上有两种选择。

With Database you basically have two options.

  1. 提供选项,用户可以将数据备份至SD卡中。
  2. 创建/使用的数据库从SD卡

案例2

您可以使用<一个href="http://developer.android.com/reference/android/content/ContextWrapper.html#getExternalFilesDir%28java.lang.String%29"相对=nofollow> getExternalFilesDir()来存储应用程序数据。这将创建在SD卡上的文件夹与您的应用程序名称。

You can use getExternalFilesDir() to store your app data. This will create a folder on SD card with your app name.

如何使用标准的 SQLLiteOpenHelper ,提供自定义路径阅读<一href="http://stackoverflow.com/questions/5332328/sqliteopenhelper-problem-with-fully-qualified-db-path-name">link.在底部的一个很好的答案。或者你可以直接使用从SD卡打开数据库<一个href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openDatabase%28java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int,%20android.database.DatabaseErrorHandler%29"相对=nofollow>的openDatabase 功能。此功能只需要路径,你的数据库。

How to use standard SQLLiteOpenHelper with custom path read this link. A nice answer at the bottom. Alternatively you can directly open your DB from SD card using openDatabase function. This function just required path to the your database.

案例1

如果您要复制数据库到SD卡,基本上是一个普通的文件副本,在这种情况下做简单的文件从一个路径到另一个。 <一href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#getPath%28%29"相对=nofollow> getPath()从SQLiteDatabase会给你的数据库的路径。

if you are copying database to sd card, which basically a regular file copy and in this case do simple file from one path to another. getPath() from SQLiteDatabase will give you path of the database.

共享preference 随着共享preferences,仅支持键值,以及如何从一个SD卡访问共享preferences阅读<一个href="http://stackoverflow.com/questions/10864462/how-can-i-backup-shared$p$pferences-to-sd-card">link.

Shared Preference With Shared preferences, which support only key values, and how to access shared preferences from a SD card read this link.

这篇关于备份/恢复应用程序数据库和Android的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆