by August 1, 2014 0 comments

What happens when you buy a new smartphone? I mean, you were already using a phone and using some apps on that phone. Yes, you can reinstall the applications. But what about the data you had for those applications? Some applications would have backed up or saved your data on to the cloud and may be able to retrieve based on your login. What about the applications you yourself write for your phone, or for your users? It would be nice if you back up the data for your application so that it comes handy when you or your user moves to a new phone or goes through factory reset of an existing phone or for any other reason loses the data for your application.

The ideal place to back up your application data is to the cloud. However, I will show you a mechanism to back up your data to a different device instead of the cloud because it is simpler that way. I would extend an Android app I have earlier written to include backup. The data I am going to backup is the SQLite database that the Android app uses. After we backup the database to a different device, not only will it be useful during an app restore, but also you can directly use the data in another device, like a Windows 7 desktop or laptop if you have SQLite installed on the same, without the Android app which created the database.

The SQLite database you create for your application is private, which means the database file cannot be accessed from outside of the application. We will now be writing code that will copy the database file to a public location on your handset. You can then send that file over Bluetooth to a different device, or copy it to a laptop/desktop over a USB port.
My previous PC Quest article described how to copy call logs about specific contacts to the calendar. The article extended that application so that the copy happens automatically through a service. I further extended that application wherein you can specify specific calling frequency to specific contacts, and the application would suggest which of your contacts is overdue for a call from you, in By now the application has a SQLite database, which we will back up in this article.

The Solution
Open the RegularContacts project that was created in the previous articles mentioned above. Open the ContactsDBOpenHelper class. Convert the class level DATABASE_NAME variable from private to public.
public static final String DATABASE_NAME = “RegularContacts.db”;
Under resources, open the values folder, and change the value of the calendar button text to Backup. Open the activity class. In the onCreate() function, define a Button variable.

Button btnCalendar;
Initialize the variable in the same function.
btnCalendar = (Button)findViewById(;
We will take the backup when the button is clicked. So let us add a click listener to the button.
btnCalendar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
We will now add the following code to the onClick() function above so that the database file is copied to the public location.
File filDir = getExternalFilesDir(null);
File filBackup = new File(filDir, ContactsDBOpenHelper.DATABASE_NAME);
try {
FileOutputStream fosBackup = new FileOutputStream(filBackup, false);
FileInputStream fisDatabase = new FileInputStream(getDatabasePath(ContactsDBOpenHelper.DATABASE_NAME));
byte[] bytBuffer = new byte[1024];
int intLength;
while ((intLength = > 0) {
fosBackup.write(bytBuffer, 0, intLength);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block

In the manifest, add the permission android.permission.WRITE_EXTERNAL_STORAGE.
Run the application. If this is the first time you have installed the application, reboot the device so that the service runs and the database is created. Run the application after the reboot and click on the Backup button. The backup would be taken.

Click on the My Files application or any other equivalent application to browse the files on the device. Open the ‘All files’ folder, then the ‘Android’ folder and next the ‘data’ folder. Check for your application folder here, along with the package name you have been using. Open the ‘files’ folder inside and here you will find the backed up file.

I subsequently connected the handset to my laptop using a USB cable. Open the phone and look for the Android folder and subsequently follow the same navigation. This is where I found the database file.
Then I copied and pasted the database file to a suitable location on my laptop.

The copied database needs to be used during a restore when the user is installing the application again. I would leave that as an exercise to you, the reader.

[image_library_tag 774/100774, alt=”quick-way-to-restore-data1″ width=”384″ height=”248″ ,default]

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.