我已使用以下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.NonWritableChannelExceptionat 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.call(AsyncTask.java:287)at java.util.concurrent.FutureTask.run(FutureTask.java:234)at androID.os.AsyncTask$SerialExecutor.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将数据库导出到SD卡-空文件全部内容,希望文章能够帮你解决Android将数据库导出到SD卡-空文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)