I have used the following example to export my db to the SD card. I'm able to create the file on the SD card, however the contents are empty. I have checked that I can read from my database file and can write to the SD source.
An exception is caught in my transfer method but the message is null
The log cat doesn't show any further information on the exception.
Any ideas why the data is not being transferred to the destination file?
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;
}
Stack trace:
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 corresponds to inChannel.transferTo(0, inChannel.size(), outChannel);