Android将数据库导出到SD卡-空文件
作者:互联网
我已使用以下example将我的数据库导出到SD卡.我可以在SD卡上创建文件,但是内容为空.我检查了是否可以从数据库文件读取并可以写入SD源.
我的传输方法中捕获了异常,但消息为空
日志猫不会显示有关该异常的任何更多信息.
有什么想法为什么不将数据传输到目标文件?
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);
解决方法:
这是一个示例,我根据时间将数据库保存到我的应用程序中的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();
关于代码,您将FileInputStream用作outChannel中的目标,请尝试将其更改为usig outChannel = new FileOutputStream(dest).getChannel();因此您实际上可以写入流.
标签:android-sqlite,android-sdcard,android 来源: https://codeday.me/bug/20191031/1973860.html