0

I am using Sql database in my appliaction,in that i want to take backup of the database.I have the following doubts:

1.I am running the application in emulator,for checking whether i have to plug some external storage to check ,if not in my system how can i check. 2.I am using the following code in my application,in that sdcard.write option is showing false,what wrong in this.

Follwing is my code:

  try {
                    File sd = Environment.getExternalStorageDirectory();
                    File data = Environment.getDataDirectory();

                    java.lang.System.out.println("data="+sd.getAbsolutePath());
 java.lang.System.out.println("data="+sd.canWrite());--->Showing as false

                    if (sd.canWrite()) {
                        String currentDBPath = "\\data\\com.budget\\databases\\budget";
                        String backupDBPath = "budget";
                        File currentDB = new File(data, currentDBPath);
                        File backupDB = new File(sd, backupDBPath);

                        java.lang.System.out.println("backup="+backupDB.getAbsolutePath());

                        if (currentDB.exists()) {
                            FileChannel src = new FileInputStream(currentDB).getChannel();
                            FileChannel dst = new FileOutputStream(backupDB).getChannel();
                            dst.transferFrom(src, 0, src.size());
                            src.close();
                            dst.close();
                        }
                    }
                } catch (Exception e) {
                }
subbu
  • 146
  • 2
  • 8

3 Answers3

0

The file extension (.db) is missing in the excerpt above:

currentDBPath = "\\\data\\\com.budget\\\databases\\\budget.db";
blackpanther
  • 10,998
  • 11
  • 48
  • 78
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
  • Thanks Harald..but in my case the if (sd.canWrite())-->itself showing as false ,so it's not entering in that if loop. – subbu May 25 '12 at 05:05
0

Data Backup Documentation in android. If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you’ll need to mess about with database files.

Below is a class called DbExportImport. Once you’ve set it up with the correct values, all you need to do is call exportDb(), importDb() or restoreDb() from your application to perform the necessary operations.

This is also useful as a temporary measure when changing your package name or key for application signing, as your application will be newly installed and you will lose your database.(More).

However, you might want to extend BackupAgent directly if you need to: * Back up data in a database. If you have an SQLite database that you want to restore when the user re-installs your application, you need to build a custom BackupAgent that reads the appropriate data during a backup operation, then create your table and insert the data during a restore operation(More).

Now your can also backup from sdcard Backup and restore SQLite database to sdcard

Here is the another solution just like your problem.

Community
  • 1
  • 1
  • thanks Google_Android_Lovers..How can i know whether a sdcard is there or not.Now i am currently running the application in emulator,so how can i check that database packup.Whether i have to plug some external memory to check.Also in my case Externalstorage state is showing as "removed". – subbu May 25 '12 at 05:33
0

If you are using Eclipse, go to Window - Android Virtual Device Manager, select the AVD you are using and click Edit, in the Hardware options select New and then select the SD Card Support. Then just define the storage size and click Edit AVD, to save the changes. To check if sd is mounted use:

if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
    //code for sd mounted
}else{
    //code for sd not mounted
}

Check if you have setted the permission in the manifest.

<manifest ...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Andre Rocha
  • 988
  • 1
  • 12
  • 21