이 프로그램은 안드로이드 애플 리케이션의 내부 스토리지에서 외부 스토리지로 "데이터베이스 이름"이라는 데이터베이스를 복사 할 수 있습니다. "폴더 이름"라는 폴더를 생성하고 "데이터베이스 이름 백업" 그 폴더 안에
1.이 프로그램은 "즉시 시스템
3.If는 다시 시작하지 않고 창은 운영 체제와 컴퓨터에서 백업 파일을 복사 할 수
2.As MediaScannerConnection를 사용하는 안드로이드 마시 멜로와 호환 폴더 이름 "이 존재하지 않으면 자동으로 생성됩니다
4. 지정한 백업 파일 이름이 > 응용 프로그램
"백업 데이터베이스 이름"내부 데이터베이스 - - 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();
}
}
}
}
}
"데이터베이스 이름"이 표시됩니다 존재> 데이터베이스를 복사 할 파일있는
"폴더 이름"-> 백업 데이터베이스를 저장할 폴더
예, 이미 해 보았습니다. 실제로 파일을 복사 한 것으로 나타 났지만 비어 있습니다. –