在我的应用程序中,我实现了通过复制 sdcard 上的数据库文件来导出数据库的可能性。在不加密文件或需要特殊密钥来读取它的情况下,我只想确保拾取文件的任何人都能够通过在 sqlite 文件阅读器中打开它来读取数据库结构或数据。
如何为我的文件实施轻量级的简单保护?
以下是我的数据库导出代码。
public void exportDB() {
final String destFolder = "/"+Constants.Files.APP_FOLDER+"/"+Constants.Files.BACKUP_FOLDER;
try {
File myBackupDir = new File(Environment.getExternalStorageDirectory() + destFolder);
if(!myBackupDir.exists() || !myBackupDir.isDirectory()) myBackupDir.mkdirs();
if(myBackupDir.isDirectory()){
File data = Environment.getDataDirectory();
if (Environment.getExternalStorageDirectory().canWrite()) {
final String currentDBPath= "//data//" + Constants.Files.PACKAGE_NAME + "//databases//" + Constants.Files.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(myBackupDir, Constants.Files.DB_NAME);
if(currentDB.isFile()){
copyFile(new FileInputStream(currentDB), new FileOutputStream(backupDB))
}
}
}
} catch (Exception e) {
Log.e(mTag, "exportDB", e);
}
}
public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
fromChannel = fromFile.getChannel();
toChannel = toFile.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
try {
if (fromChannel != null) {
fromChannel.close();
}
} finally {
if (toChannel != null) {
toChannel.close();
}
}
}
}