Santiago
Santiago

Reputation: 982

Android export database to SD card - empty file

I have used the following example to export my db to the SD card. I'm able to create the file on the SD card, however the contents are empty. I have checked that I can read from my database file and can write to the SD source.

An exception is caught in my transfer method but the message is null

The log cat doesn't show any further information on the exception.

Any ideas why the data is not being transferred to the destination file?

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

Stack trace:

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 corresponds to inChannel.transferTo(0, inChannel.size(), outChannel);

Upvotes: 3

Views: 1943

Answers (3)

hardartcore
hardartcore

Reputation: 17037

Here is an example which I am using to save the database to SD Card in my apps depending on the time because sometimes you need to have different copies of your database for many reasons (at least in my situation) :

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

And about your code you are using FileInputStream as destination in outChannel, try to change it usig outChannel = new FileOutputStream(dest).getChannel(); so you can actually write to the stream.

Upvotes: 3

itsrajesh4uguys
itsrajesh4uguys

Reputation: 4638

Do you have memory card in your mobile ?

else if Memory Card is present in your phone checkit whether its mounted?

And also another possibility is you don't have permission to write a file in your sdcard..

Hope this will help you.

You have a done the wrong in your code. inChannel = new FileInputStream(src).getChannel(); outChannel = new FileInputStream(dest).getChannel();

this is for reading purpose not for writing purpose. need to change it as FileoutputSream.

Upvotes: 0

Santiago
Santiago

Reputation: 982

Ah, found the issue.

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

This should be:

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

Upvotes: 1

Related Questions