我使用以下示例将我的数据库导出到 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);