3

我使用以下示例将我的数据库导出到 SD 卡。我可以在 SD 卡上创建文件,但是内容是空的。我已经检查过我可以从我的数据库文件中读取数据并且可以写入 SD 源。

在我的转移方法中发现了一个异常,但消息是null

日志猫没有显示有关异常的任何进一步信息。

任何想法为什么数据没有被传输到目标文件?

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);

4

3 回答 3

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();

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

于 2013-03-01T14:13:49.843 回答
1

Ah, found the issue.

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

This should be:

outChannel = new FileOutputStream(dest).getChannel();
于 2013-03-01T14:17:30.140 回答
0

你的手机里有存储卡吗?

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

还有另一种可能性是你没有权限在你的 sdcard 中写入文件..

希望这会帮助你。

您在代码中做错了。inChannel = new FileInputStream(src).getChannel(); outChannel = new FileInputStream(dest).getChannel();

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

于 2013-03-01T14:35:41.590 回答