ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Android将数据库导出到SD卡-空文件

2019-10-31 07:27:16  阅读:238  来源: 互联网

标签:android-sqlite android-sdcard android


我已使用以下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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有