// 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();
}

 

private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
      File data = Environment.getDataDirectory();
       FileChannel source=null;
       FileChannel destination=null;
       String currentDBPath = "/data/"+ "com.authorwjf.sqliteexport" +"/databases/"+DB_NAME;
       String backupDBPath = DB_NAME;
       File currentDB = new File(data, currentDBPath);
       File backupDB = new File(sd, backupDBPath);
       try {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
        } catch(IOException e) {
        e.printStackTrace();
        }
}

'Andorid' 카테고리의 다른 글

Export SQLite DB  (0) 2019.02.22
Android Emulators  (0) 2018.03.02
Android 에서 각종 아이콘이 표시 결과 확인하기  (0) 2017.12.30
Google Play 게임 만들때 읽어야할 글  (0) 2017.12.24
google-play-services(libproject)  (0) 2017.12.08
FCM 과 GCM 차이? - 펌  (0) 2017.11.25

+ Recent posts