0
在我的應用程序中,我已經實現了通過在SD卡上覆制數據庫文件來導出數據庫的可能性。 沒有加密文件或需要特殊的密鑰來讀取它,我只是想確保任何人選擇文件,只要通過在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();
}
}
}
}
這是用戶的數據,不是你的。請允許用戶通過用戶的數據來執行用戶想要的操作。 – CommonsWare 2013-03-22 12:01:58