该程序可以复制指定的数据库“数据库名称”从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