[Android]SQLite3 db Export
必要。
Manifast.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
コード。
public void sqliteExport(String dbName, String exportFileName) {
try {
File data = Environment.getDataDirectory();
File sd = Environment.getExternalStorageDirectory();
Log.e(TAG, "getDataDirectory: - " + data.toString());
Log.e(TAG, "getExternalStorageDirectory: - " + sd.toString());
if (sd.canWrite()) {
String currentDBPath = "/data/pakagename/databases/" + dbName;
String backupDBPath = "/app_name/data/files/" + exportFileName;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
if (backupDB.exists()) {
//Toast.makeText(this, "DB Export Complete!!", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Manifast.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public void sqliteExport(String dbName, String exportFileName) {
try {
File data = Environment.getDataDirectory();
File sd = Environment.getExternalStorageDirectory();
Log.e(TAG, "getDataDirectory: - " + data.toString());
Log.e(TAG, "getExternalStorageDirectory: - " + sd.toString());
if (sd.canWrite()) {
String currentDBPath = "/data/pakagename/databases/" + dbName;
String backupDBPath = "/app_name/data/files/" + exportFileName;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
if (backupDB.exists()) {
//Toast.makeText(this, "DB Export Complete!!", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Author And Source
この問題について([Android]SQLite3 db Export), 我々は、より多くの情報をここで見つけました https://qiita.com/Dreamwalker/items/185aaf1cb383a2f7e69a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .