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

时间:2013-03-01 12:19:37

标签: android android-sqlite android-sdcard

我使用以下example将我的数据库导出到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)

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

3 个答案:

答案 0 :(得分:3)

以下是我用来根据时间将数据库保存到我的应用程序中的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();

关于您在outChannel中使用FileInputStream作为目标的代码,尝试使用outChannel = new FileOutputStream(dest).getChannel();更改它,以便您可以实际写入流。

答案 1 :(得分:1)

啊,发现了这个问题。

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

这应该是:

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

答案 2 :(得分:0)

手机中有存储卡吗?

否则,如果您的手机中存在存储卡,请检查其是否已安装?

另一种可能性是你没有权限在你的SD卡中写一个文件..

希望这会对你有所帮助。

你的代码中有错误。           inChannel = new FileInputStream(src).getChannel();             outChannel = new FileInputStream(dest).getChannel();

这是出于阅读目的而非出于写作目的。需要将其更改为FileoutputSream。