且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

备份/恢复应用程序数据库和Android的设置

更新时间:2023-01-09 12:28:55

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

With Database you basically have two options.

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

案例2

您可以使用 getExternalFilesDir()来存储应用程序数据。这将创建在SD卡上的文件夹与您的应用程序名称。

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

如何使用标准的 SQLLiteOpenHelper ,提供自定义路径阅读link.在底部的一个很好的答案。或者你可以直接使用从SD卡打开数据库的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卡,基本上是一个普通的文件副本,在这种情况下做简单的文件从一个路径到另一个。 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阅读link.

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