728x90

// selectPath : SQLite DB 파일 백업 폴더 위치

Java.IO.File sd = new Java.IO.File(selectPath);

// 폴더가 존재하지 않으면 만든다.

if (!sd.Exists())
{
    sd.Mkdirs();
}

Java.IO.File data = Android.OS.Environment.DataDirectory;
// System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);


FileChannel source = null;
FileChannel destination = null;

//  data 가 두번 붙는거 방지 (안드로이드 버전에 따라 다르게 나오는 부분 처리)

List<string> pathList = Constant.DBPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList();

if (pathList[0] == "data")
{
    pathList.RemoveAt(0);
}

string path = string.Join("/", pathList);

string currentDBPath = path;

string backupDBPath = "test.db";
Java.IO.File currentDB = new Java.IO.File(data, currentDBPath);
Java.IO.File backupDB = new Java.IO.File(sd, backupDBPath);
try
{
    source = new FileInputStream(currentDB).Channel;
    destination = new FileOutputStream(backupDB).Channel;
    destination.TransferFrom(source, 0, source.Size());
    source.Close();
    destination.Close();
    Toast.MakeText(this, "백업 완료! - " + backupDB.Path, ToastLength.Long).Show();
}
catch (Java.Lang.Exception ex)
{
    ex.PrintStackTrace();
}

 

728x90
Posted by kjun.kr
,