次の例を使用して、データベースをSDカードにエクスポートしました。SDカードにファイルを作成できましたが、中身が空です。データベースファイルから読み取り、SDソースに書き込めることを確認しました。
転送メソッドで例外がキャッチされましたが、メッセージはnull
log catは、例外に関するそれ以上の情報を表示しません。
データが宛先ファイルに転送されない理由はありますか?
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);に対応します。