2013-07-03 7 views
0

환경 설정 활동에서 외부 sdCard로 앱 데이터베이스를 백업/복원하려고합니다. 데이터베이스를 외부 sdCard에 저장할 수 있었지만 이제이 파일을 기본 경로 (data \ package.app \ databases)로 다시 전송할 수있는 방법을 모르겠습니다. 어떤 생각?안드로이드에서 sdcard로 백업/복원

답변

3

나는 이런 식으로 작업을 수행합니다

수출 :

InputStream myInput; 
      String dbpath = "/data/"+pckgname+"/databases/refuel_db"; 
      String sdpath = Environment.getExternalStorageDirectory().getPath(); 

      try { 

       myInput = new FileInputStream(Environment.getDataDirectory() 
         + dbpath); 


       // Set the output folder on the Scard 
       File directory = new File(sdpath + "/Refuel"); 
       // Create the folder if it doesn't exist: 
       if (!directory.exists()) { 
        directory.mkdirs(); 
       } 
       // Set the output file stream up: 

       OutputStream myOutput = new FileOutputStream(directory.getPath() 
         + "/refuel_db"); 

       // Transfer bytes from the input file to the output file 
       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = myInput.read(buffer)) > 0) { 
        myOutput.write(buffer, 0, length); 
       } 
       // Close and clear the streams 

       myOutput.flush(); 

       myOutput.close(); 

       myInput.close(); 

       Toast.makeText(getActivity(), "Backup Done Succesfully!", Toast.LENGTH_LONG) 
         .show(); 

      } catch (FileNotFoundException e) { 
       Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show(); 

       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show(); 

       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

가져 오기 :

OutputStream myOutput; 

      String dbpath = "/data/"+pckgname+"/databases/refuel_db"; 
      String sdpath = Environment.getExternalStorageDirectory().getPath(); 

      try { 

       myOutput = new FileOutputStream(Environment.getDataDirectory() 
         + dbpath); 

       // Set the folder on the SDcard 
       File directory = new File(sdpath + "/Refuel"); 
       // Set the input file stream up: 

       InputStream myInputs = new FileInputStream(directory.getPath() 
         + "/refuel_db"); 

       // Transfer bytes from the input file to the output file 
       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = myInputs.read(buffer)) > 0) { 
        myOutput.write(buffer, 0, length); 
       } 

       // Close and clear the streams 
       myOutput.flush(); 

       myOutput.close(); 

       myInputs.close(); 

       Toast.makeText(getActivity(), "Import Done Succesfully!", Toast.LENGTH_LONG) 
         .show(); 

      } catch (FileNotFoundException e) { 
       Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show(); 

       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show(); 

       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
+0

감사합니다, – user1477747

+0

는 비 루트 장치에 대한 작동합니까 정말 helpul? – angel

+0

@angel 물론 물론 –

관련 문제