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

Android将数据库导出到SD卡-空文件,android,android-sqlite,android-sdcard,Android,Android Sqlite,Android Sdcard,我已使用以下方法将我的db导出到SD卡。我可以在SD卡上创建文件,但是内容是空的。我已检查是否可以从数据库文件读取数据,并可以写入SD源 在我的传输方法中捕获到异常,但消息为null 日志cat不会显示有关异常的任何进一步信息 知道为什么数据没有传输到目标文件吗 public boolean transferFile(File src, File dest){ boolean flag = false; FileChannel inChannel=null;;

我已使用以下方法将我的db导出到SD卡。我可以在SD卡上创建文件,但是内容是空的。我已检查是否可以从数据库文件读取数据,并可以写入SD源

在我的传输方法中捕获到异常,但消息为
null

日志cat不会显示有关异常的任何进一步信息

知道为什么数据没有传输到目标文件吗

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)

java:250对应于inChannel.transferTo(0,inChannel.size(),outChannel)

下面是一个示例,我使用它将数据库保存到我的应用程序中的SD卡,具体取决于时间,因为有时出于多种原因(至少在我的情况下),您需要有不同的数据库副本:

关于您在
outChannel
中使用FileInputStream作为目标的代码,请尝试使用
outChannel=newfileoutputstream(dest).getChannel()更改它这样您就可以实际写入流了。

啊,找到问题了

outChannel = new FileInputStream(dest).getChannel();
这应该是:

outChannel = new FileOutputStream(dest).getChannel();

你的手机里有存储卡吗

否则,如果手机中有存储卡,请检查其是否已安装

还有一种可能是,您没有在SD卡中写入文件的权限

希望这对你有帮助

您的代码中有一个错误。 inChannel=newfileinputstream(src).getChannel(); outChannel=newfileinputstream(dest).getChannel()


这是为了阅读而不是为了写作。需要将其更改为FileoutputSream。

将stacktrace粘贴到此处。您提供的包和数据库名称正确吗?@Real stack traceadded@itsrajesh4uguys是,在调用传输文件方法之前,我已检查了源文件和目标文件夹。没有错误。嗨,我已经检查了你提到的相同代码。。但我正在成功地获取文件。。。进入我的SD卡。。但是我改变了两个参数。。不管怎样,我都会发布代码…是的,看代码太久了,错过了。谢谢你能解释一下你的解决方案吗?
outChannel = new FileOutputStream(dest).getChannel();