Copy database file to sdcard

19 Jan

If your db file resides at location “/data/data/” then you will not be able to access that file from your device and before doing that you need to copy the same db file from particular location to the sdcard. And here is the example which will help you for doing so…

public void copyDataBase() throws IOException {
String inputFileName;
FileInputStream myInput = null;
OutputStream myOutput = null;
byte[] buffer;
// Open your local db as the input stream
inputFileName = context.getDatabasePath(DATABASE_NAME.toString()).getAbsolutePath();
myInput = new FileInputStream(inputFileName);

// Path to the just created empty db
String outFileName;

// Open the empty db as the output stream
myOutput = new FileOutputStream(outFileName);

// Dump all the data to file line by line
buffer = new byte[1024];
int length;
while ((length = > 0) {
myOutput.write(buffer, 0, length);
catch (Exception e) {
// Close the streams
buffer = null;
Leave a comment

Posted by on January 19, 2013 in Android Database


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: