user3245658
user3245658

Reputation: 93

How to retrieve backup of database from app?

I have trying to copy my app database from storage data to any outher floder but i'm getting some error .

my device is rooted and i have used many apps from google play to nevigate to data/data but it's empty and i have used this code :

 public void backup() {
        try {
            File sdcard = Environment.getRootDirectory();
            File outputFile = new File(sdcard,
                    "YourDatabase.db");

            if (!outputFile.exists()) 
                 outputFile.createNewFile(); 

            File data = Environment.getDataDirectory();
            File inputFile = new File(data,
                    "data/"+LoginActivity.this.getPackageName()+"/databases/"+"HafilTC.db");
            InputStream input = new FileInputStream(inputFile);
            OutputStream output = new FileOutputStream(outputFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new Error(e.toString());
        }

android mainfilfist :

     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

error log :

02-11 18:38:16.213: E/AndroidRuntime(21338): java.lang.Error: java.io.IOException: open failed: EROFS (Read-only file system)

Upvotes: 1

Views: 137

Answers (3)

user3245658
user3245658

Reputation: 93

The problem has been solved by remove .db from HafilTC.db

to make it just file name : HafilTC

Upvotes: 0

Bryan Herbst
Bryan Herbst

Reputation: 67209

The problem is that Environment.getRootDirectory() does not return the root directory of the SD card. It returns the root OS directory, which you cannot write to.

You should instead use Environment.getExternalStorageDirectory(), which will return the root directory of the user's external storage directory (which may or may not be an SD Card, depending on the device).

Upvotes: 1

elbuild
elbuild

Reputation: 4899

You have at least two options here.

Using DDMS

Open Eclipse, and switch to DDMS perspective.

Than follow the path:

DDMS--> file explorer-->data--> data--> your package name-->databases

Your database should be inside databases folder, and since you're on a rooted device you can get it.

Using ADB

You can use ADB once you have a device connected via USB and with USB debig enabled. To check if your device is correctly connected use:

adb devices

Than get your DB with these commands (you're copying it to SDCARD, and then reading directly from there):

adb -d shell
run-as your.package.name
cat /data/data/your.package.name/databases/database_name.db > /sdcard/your_db_name.db

If you want to do it programmatically have a look to what @Tanis.7x suggests.

Upvotes: 0

Related Questions