Kopieren einer Datenbank an einen verschlüsselten Speicherort
Wenn Sie eine SQLite-Datenbank an einen verschlüsselten Speicherort auf einem BlackBerry-Smartphone (eMMC- oder microSD-Karte) kopieren, kann die Datenbank-API u. U. nicht mehr auf die Datenbank zugreifen. Dies geschieht, weil die Medienkarte eine andere Art von Verschlüsselung als die Datenbank-API verwendet.
Um eine Datenbank so zu kopieren, dass Sie auf sie zugreifen können, sollten Sie die Datenbank-API verwenden, um eine leere Datenbankdatei zu erstellen, die Datei auf null verkleinern und anschließend die Datenbank in die Datei kopieren.
Im folgenden Codebeispiel wird dieses Verfahren vorgeführt.
// Copy a database to an encrypted storage location.
private void copyDBRecommendedWay() {
try {
String dbPath = "file:///SDCard/original.db";
String dbPathCopy = "file:///SDCard/copy.db";
Database dbCopy = null;
try {
// Delete if there's an existing one.
DatabaseFactory.delete(URI.create(dbPathCopy));
// Create the database using the DatabaseFactory class.
dbCopy = DatabaseFactory.create(URI.create(dbPathCopy));
} catch (DatabaseException e) {
System.out.println( "DatabaseException: error code: "
+ e.getErrorCode() + " msg: " + e.getMessage());
} finally {
// Close the database.
dbCopy.close();
}
// Open a connection to the database file that was created.
FileConnection outfc = (FileConnection)Connector.open(dbPathCopy);
// Truncate the file.
outfc.truncate(0);
// Write out the downloaded database data to FileConnection.openOutputStream().
OutputStream os = outfc.openOutputStream();
FileConnection infc = (FileConnection)Connector.open(dbPath);
InputStream is = infc.openInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0){
os.write(buf, 0, len);
}
is.close();
os.close();
// Close the file connection.
outfc.close();
System.out.println("Copied " + dbPath + " to " + dbPathCopy);
// The database can now be reopened with the DatabaseFactory class.
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
Waren diese Informationen hilfreich? Senden Sie uns Ihren Kommentar.