RSS

Copy database file to sdcard

19 Jan

If your db file resides at location “/data/data/com.company.projectname/databases” 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;
try{
// 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;
outFileName="sdcard/"+DATABASE_NAME;

// 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 = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
}
catch (Exception e) {
e.toString();
}
finally{
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
buffer = null;
}
}
Advertisements
 
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: