2012-11-20 2 views
0

Possible Duplicate:
Moving my db to sd card not working내가 내 SDCARD에 sqlite가 파일을 저장하기 위해 노력하고있어

sdcard에하는 SQLite는 DB를 저장하려고합니다. 나는이 매우 높은 평가 대답이 question

try { 
    File sd = Environment.getExternalStorageDirectory(); 
    File data = Environment.getDataDirectory(); 

    if (sd.canWrite()) { 
     String currentDBPath = "\\data\\com.test.mytest\\databases\\test_db"; 
     String backupDBPath = "test_db"; 
     File currentDB = new File(data, currentDBPath); 
     File backupDB = new File(sd, backupDBPath); 

     if (currentDB.exists()) { 
      FileChannel src = new FileInputStream(currentDB).getChannel(); 
      FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
      dst.transferFrom(src, 0, src.size()); 
      src.close(); 
      dst.close(); 
     } 
    } 
} catch (Exception e) { 
} 

에서 코드를 사용하고, 그래서 그것을 꽤 단순히 작동해야한다고 생각합니다. 나는 logcat에서 오류가 발생하지 않습니다. 내가 만든 디렉토리/생성 된 파일이 보이지 않습니다. 또한 내 매니페스트에 "외부에 쓰기"권한이 있습니다.

+1

단순히 예외를 잡는 대신 예외를 기록하여 시작하십시오. 물론 catch (Exception e) {}'는 로그에 아무 것도 없습니다. – njzk2

+0

작업 중간에 오류가 발생하면 오류로 기록되지 않습니까? – EGHDK

+0

예외가 발생했을 때 catch 블록에 의해 catch되고 catch 블록이 비어 있으므로 아무 것도 수행되지 않습니다. 적어도 e.printStackTrace()를 넣으십시오. – njzk2

답변

0

돈 t amnifest 파일

에 권한을 선언하는 것을 잊지 것은 사용 - 권한 안드로이드 : 이름 = "android.permission.WRITE_EXTERNAL_STORAGE"

+0

내 질문의 맨 아래에 "또한 내 매니페스트에"외부에 쓰기 "권한이 있습니다." – EGHDK

1

파일 SD = Environment.getExternalStorageDirectory(); 파일 데이터 = Environment.getDataDirectory();

  if (sd.canWrite()) { 
      String currentDBPath = "/data/packagename/databases/DATABASENAME"; 
      String backupDBPath = "/backup/"+"main.db"; 
      File currentDB = new File(data, currentDBPath); 
      File backupDB = new File(sd, backupDBPath); 

      if (currentDB.exists()) { 
       FileChannel src = new FileInputStream(currentDB).getChannel(); 
       FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
       dst.transferFrom(src, 0, src.size()); 
       src.close(); 
       dst.close(); 
      } else { 
       Log.e(TAG, "File does not exist: " + currentDBPath); 
      } 
+0

이 코드는 나를 위해 charme처럼 작동합니다. 거의 동일한 maye가 sdcard의 경로가 될 수 있습니다. – jnr

+0

오류 발생 : http://s16.postimage.org/sx0p2beyb/stack.png – EGHDK

+0

심각하게? 이것은 당신이 실수를하는 방법입니까? –

관련 문제