該程序可以複製指定的數據庫「數據庫名稱」從Android應用程序與外部storage.It的內部存儲創建一個名爲「文件夾名稱」,然後將數據庫複製到一個名爲「備份數據庫名稱」文件夾該文件夾內
1.本程序與Android兼容棉花糖
2.As MediaScannerConnection使用,可以立即從電腦windows操作系統的複製備份文件而無需重新啓動您的系統
3.如果「文件夾名稱「不存在它將自動創建它
4.如果您指定的備份文件名稱爲a lready存在,它會顯示一個警告對話框
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
}
else{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},5);
}
}
else{
final File backupDb=new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>"+File.separator+"<Backup Database Name>");
final File currentDB = new File(String.valueOf(getApplicationContext().getDatabasePath("<Your Database Name>")));
if(backupDb.exists()){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert")
.setMessage("File already exists.Do you want to replace it")
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(!new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>").canWrite()) {
Toast.makeText(MainActivity.this, "Unable to write into external storage", Toast.LENGTH_SHORT).show();
}
else{
if(!currentDB.exists()){
Toast.makeText(MainActivity.this, "Database doesn't exists", Toast.LENGTH_SHORT).show();
}
else{
try {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDb).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{backupDb.toString()}, null, null);
Toast.makeText(MainActivity.this, "Database successfully copied to external storage", Toast.LENGTH_SHORT).show();
editText.setText(null);
}catch(Exception e){
Toast.makeText(MainActivity.this, "Got exception" + e, Toast.LENGTH_SHORT).show();
}
}
}
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Please enter another name", Toast.LENGTH_SHORT).show();
}
})
.setIcon(R.drawable.alert);
AlertDialog dialog = builder.create();
dialog.show();
}
else{
new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>").mkdir();
if(!new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"<Folder Name>").canWrite()) {
Toast.makeText(MainActivity.this, "Unable to write into external storage", Toast.LENGTH_SHORT).show();
}
else{
if(!currentDB.exists()){
Toast.makeText(MainActivity.this, "Database doesn't exists", Toast.LENGTH_SHORT).show();
}
else{
try {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDb).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{backupDb.toString()}, null, null);
editText.setText(null);
Toast.makeText(MainActivity.this, "Database successfully copied to external storage", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Toast.makeText(MainActivity.this, "Got exception" + e, Toast.LENGTH_SHORT).show();
}
}
}
}
}
「數據庫名稱」 - >數據庫內部應用程序
「備份數據庫名稱」 - >文件中,你要複製的數據庫
「文件夾名稱」 - >將備份數據庫文件夾存儲
是的,我已經這樣做。其實我注意到文件被複制,但是它是空的。 – 2012-01-30 16:27:51