2011-10-12 3 views
1

안드로이드 폰에 데이터베이스가 있는데, SD 카드에 정보를 가져와야합니다.데이터베이스 파일을 SD 카드에 복사 할 수 있습니까?

읽을 수있는 상태로 데이터베이스 파일을 SD 카드에 저장할 수 있습니까? 이 작업을 수행하는 방법에 대한 정보를 찾을 수 없었습니다.

데이터베이스 파일을 SD 카드에 복사하는 일부 소스 코드가 이상적입니다.

+0

ADK를 설치하고 ADB를 사용하여 이동/당깁니다. – Rob

+0

입력 Rob에 감사드립니다. 그러나 그것은 내가 전화를 끊을 필요가있는 전화에 대해 루트 허가를 요구할 것입니다. 그렇지 않으면 완벽한 해결책이었을 것입니다. – Dave

답변

2

데이터베이스 파일은 다른 파일과 같습니다. 바이너리 파일 복사본을 만들면 작동합니다. 당신이 사용할 수 있도록

자바는 더, 파일 복사 방법에 내장 없다 :
Permission to write to the SD card

:

Standard concise way to copy a file in Java?

그냥 SD 카드에 쓸 수있는 매니페스트 권한을 추가하는 것을 잊지 마세요

+0

감사합니다. LAS_VEGAS! 당신이 올바른지. 내가 생각했던 것보다 쉬워졌다. – Dave

0

다음은 내가 다른 여러 사용자로부터 침탈당한 스크립트입니다. 파일을 저장할 위치를 Android에 알릴 수있는 것처럼 보이지만 adb 쉘로 전화를 걸면 찾기 힘들어 질 수 있습니다!

이 코드 (디버깅을 위해 작업 표시 줄의 임시 단추에 매핑 한 코드)는 "database saved to : /storage/emulated/0/DB-DEBUG/todotable.db"와 같이 인쇄되지만 내 휴대 전화의 셸은 실제로 내 데이터베이스를 찾았습니다. "/ storage/emulated/legacy/DB-DEBUG /"... 확실하지 않은 점이 있지만 이제는 sqlite 브라우저로 데이터베이스를 체크 아웃 할 수 있습니다!

//db will reside in: /storage/emulated/legacy/DB_DEBUG 
private void copyDatabase(Context c, String DATABASE_NAME) { 
    String databasePath = c.getDatabasePath(DATABASE_NAME).getPath(); 
    File f = new File(databasePath); 
    OutputStream myOutput = null; 
    InputStream myInput = null; 
    Log.d("testing", " testing db path " + databasePath); 
    Log.d("testing", " testing db exist " + f.exists()); 

    if (f.exists()) { 
     try { 
      File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DB-DEBUG"); 
      if (!directory.exists()){ 
       directory.mkdir(); 
      } 

      String copyPath = directory.getAbsolutePath() + "/" + DATABASE_NAME; 
      myOutput = new FileOutputStream(copyPath); 
      myInput = new FileInputStream(databasePath); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 

      myOutput.flush(); 
      Toast.makeText(getBaseContext(), "Your database copied to: " + copyPath, Toast.LENGTH_LONG).show(); 
      Log.d("testing", " database saved to: " + copyPath); 

     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 

     } finally { 
      try { 
       if (myOutput != null) { 
        myOutput.close(); 
        myOutput = null; 
       } 
       if (myInput != null) { 
        myInput.close(); 
        myInput = null; 
       } 
      } catch (Exception e) { 
       Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 
관련 문제