2013-03-01 2 views
3

다음은 example을 사용하여 db를 SD 카드로 내보내는 방법입니다. SD 카드에 파일을 만들 수 있지만 내용은 비어 있습니다. 데이터베이스 파일을 읽을 수 있는지 확인하고 SD 소스에 쓸 수 있습니다.안드로이드 수출 데이터베이스 - SD 카드 - 빈 파일

예외가 내 전송 방법에 걸려 있지만 메시지는 로그 고양이 예외에 대한 추가 정보가 표시되지 않습니다 null

입니다.

데이터가 대상 파일로 전송되지 않는 이유는 무엇입니까?

public boolean transferFile(File src, File dest){ 
     boolean flag = false; 
     FileChannel inChannel=null;; 
     FileChannel outChannel=null; 
     if(!dest.canWrite()){ 
      Log.d(TAG, "unable to write to: " + dest.getPath()); 
       return false; 
     } 

     try{ 
      inChannel = new FileInputStream(src).getChannel(); 
      outChannel = new FileInputStream(dest).getChannel(); 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
      //outChannel.transferFrom(inChannel, 0, inChannel.size()); 
      flag = true; 

      if(inChannel !=null){ 
       inChannel.close(); 
      } 
      if(outChannel !=null){ 
       outChannel.close(); 
      } 
     } catch (IOException e){ 
      Log.d(TAG, "Unable to transfer file IOException: " + e.getMessage()); 
     } catch (Exception e){ 
      Log.d(TAG, "Unable to transfer file Exception: " + e.getMessage()); 
     } 
     return flag; 
    } 

스택 트레이스 :

transferFile() Unable to transfer file Exception: java.nio.channels.NonWritableChannelException 
at java.nio.FileChannelImpl.checkWritable(FileChannelImpl.java:85) 
at java.nio.FileChannelImpl.transferTo(FileChannelImpl.java:399) 
at com.test.activity.TestActivity$ExportDBTask.transferFile(TestActivity.java:250) 
at com.test.activity.TestActivity$ExportDBTask.doInBackground(TestActivity.java:187) 
at com.test.activity.TestActivity$ExportDBTask.doInBackground(TestActivity.java:1) 
at android.os.AsyncTask$2.call(AsyncTask.java:287) 
at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 

TestActivity.java:250 inChannel.transferTo는 (0, inChannel.size() outChannel)에 대응하고;

+0

붙여 넣기 스택 트레이스는 여기에 있습니다. –

+0

패키지 및 db 이름을 올바르게 입력 했습니까? – itsrajesh4uguys

+0

@Real 스택 추적 추가 – Santiago

답변

1

아, 문제가 발견되었습니다.

outChannel = new FileInputStream(dest).getChannel(); 

이 있어야한다 :

outChannel = new FileOutputStream(dest).getChannel(); 
+2

해결책을 설명해 주시겠습니까? –

3

다음은 여러 상황 (적어도 내 상황에서는)에 따라 데이터베이스의 복사본이 필요하기 때문에 시간에 따라 내 앱의 SD 카드에 데이터베이스를 저장하는 예제입니다.

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

    Calendar c = Calendar.getInstance(); 
    int year = c.get(Calendar.YEAR); 
    int month = c.get(Calendar.MONTH) + 1; 
    int day = c.get(Calendar.DAY_OF_MONTH); 
    int hours = c.get(Calendar.HOUR); 
    int minute = c.get(Calendar.MINUTE); 
    int second = c.get(Calendar.SECOND); 

    String currentDBPath = "/data/your.package.name/databases/database.sqlite"; 
    String backUpSystemData = "myDatabase-" + year + "-" + month + "-" + day + "-" + hours + "-" + minute + "-" + second + ".sqlite"; 
    File currentDB = new File(data, currentDBPath); 
    File path = new File(sd + "/.MyDatabase/Database/"); 
    if(!path.exists()){ 
     path.mkdirs(); 
    } 
    File backupDB = new File(path, backUpSystemData); 
    FileChannel src = new FileInputStream(currentDB).getChannel(); 
    FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
    dst.transferFrom(src, 0, src.size()); 
    src.close(); 
    dst.close(); 

코드에 대해 outChannel에서 FileInputStream을 대상으로 사용하는 경우 실제로 변경 내용을 스트림에 쓸 수 있도록 변경하십시오. outChannel = new FileOutputStream(dest).getChannel();

+0

네, 너무 오랫동안 코드를보고 그걸 놓쳤습니다. 감사합니다 – Santiago

0

하는 모바일 메모리 카드를 가지고 있습니까?

다른 경우 메모리 카드가 휴대 전화에 있으면 카드가 마운트되어 있는지 확인하십시오. 이 당신을 도울 것입니다

그리고 또 다른 가능성은 당신이 당신의 sdcard에 파일을 쓸 수있는 권한이 없습니다이다

..

희망.

코드에 잘못이 있습니다. inChannel = 새 FileInputStream (src) .getChannel(); outChannel = 새 FileInputStream (dest) .getChannel();

이것은 쓰기 목적이 아닌 읽기 전용입니다. FileoutputSream로 변경해야합니다.

관련 문제