How to backup Android Room Database file to external storage

In this android programming source code example, we are going to backup Android Room Database file to external storage.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to backup Android Room Database file to external storage.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="15dp">

    <Button
        android:id="@+id/backupBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_centerInParent="true"
        android:textColor="#ffffff"
        android:text="Backup to Sdcard"/>


</RelativeLayout>

MainActivity.java

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class RoomDatabaseActivity6 extends AppCompatActivity {
    private Button backupBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_database6);
        backupBtn = (Button) findViewById(R.id.backupBtn);
        backupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backup();
            }
        });
    }

    private void backup(){
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            if(sd.canWrite()){
                String currentDbPath = "//data//com.bluapp.androidview//databases//UserDb";
                String backupPath = "backup.db";
                File currentDb = new File(data, currentDbPath);
                File backupDb = new File(sd, backupPath);
                FileChannel src = new FileInputStream(currentDb).getChannel();
                FileChannel dst = new FileOutputStream(backupDb).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(RoomDatabaseActivity6.this,"Backup successfully",Toast.LENGTH_LONG).show();
            }

        }catch (Exception e){
            Log.e("Error", e.getMessage());
        }
    }
}

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment