Krishnakant Dalal Krishnakant Dalal - 1 year ago 102
Android Question

Copy Database from assets folder in unrooted device

I am trying to copy DB from assets folder to device. This code is working fine on Emulator and rooted Device. I just want to know is it create any problem on unrooted device or it will work same.

private void StoreDatabase() {
File DbFile = new File(
if (DbFile.exists()) {
System.out.println("file already exist ,No need to Create");
} else {
try {
System.out.println("File Created successfully");
InputStream is = this.getAssets().open("DBname.sqlite");
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = > 0) {
fos.write(buffer, 0, length);
System.out.println("File succesfully placed on sdcard");
// Close the streams
} catch (IOException e) {


Answer Source

This will work for sure in all devices and emulator , no need to root.

     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * */
    private void copyDataBase(String dbname) throws IOException {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(dbname);
        // Path to the just created empty db
        File outFileName = getDatabasePath(dbname);
        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = > 0) {
            myOutput.write(buffer, 0, length);
        // Close the streams
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download