Saturday, 28 June 2014

Copy Database Into SD Card From Android Application


In my last Database(Tutorial 1) I described you how to insert thousands of data in database. In emulator you can easily fetch the database with the help of DDMS, but from actual device you can't fetch database from DDMS. So with reference to my last Database(Tutorial 1) tutorial I will show you how to copy Database into SD Card in actual device.


Download Source Code Download


Firstly we require permission for writing in SD card. So write this permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Now create a UtilFunctions.java class in your package , and make a copyDatabase function-

UtilFunctions.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import android.content.Context;
import android.os.Environment;
import com.org.database.DatabaseConstant;

public class UtilFunctions {

      public static void copyDatabase(Context context) {
            try {
                  String DATABASE_SD_DIRS = "/databaseDemo/database";   //Folder in SD Card
                  String dbName = System.currentTimeMillis() + "";     
                  //Current Database Path in application
                  String currentDatabasePath = context.getDatabasePath(DatabaseConstant.DB_NAME).toString();
                  //Storing Path in SD Card
                  String path = Environment.getExternalStorageDirectory().getAbsolutePath() + DATABASE_SD_DIRS;
                  File sdCardFile = new File(path, dbName);
                  File dir = new File(path);
                  if (!dir.exists()) {
                        dir.mkdirs();
                  }
                  if (sdCardFile.exists()) {
                        sdCardFile.delete();
                  }
                  File currentDB = new File(currentDatabasePath);
                  if (currentDB.exists()) {
                        FileChannel src = new FileInputStream(currentDB).getChannel();
                        FileChannel dst = new FileOutputStream(sdCardFile).getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                  }
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
} 

Now just pass the context as a parameter of copyDatabase function and it will copy the database in the path provided.

According to docs-
  • FileChannel : An abstract channel type for interaction with a platform file.A FileChannel defines the methods for reading, writing, memory mapping, and manipulating the logical state of a platform file.
  • FileInputStream : Constructs a new FileInputStream that reads from file.
  • FileOutputStream : Constructs a new FileOutputStream that writes to file. The file will be truncated if it exists, and created if it doesn't exist.
  • transferFrom() : Reads up to count bytes from src and stores them in this channel's file starting at position. No bytes are transferred if position is larger than the size of this channel's file. Less than count bytes are transferred if there are less bytes remaining in the source channel or if the source channel is non-blocking and has less than count bytes immediately available in its output buffer.

1 comment: