2

I've been trying to create a copy of the applications database and save it onto the root of the phone, but no luck. My logs go:

STEP ONE

STEP DEAD

Apparently if (sd.canWrite()) is the first mistake? Can anyone help with this? I'm running out of ideas. Thanks

public void save() {


        File data = Environment.getDataDirectory();
        File sd = new File(Environment.getExternalStorageDirectory().toString());
        sd.mkdirs();
        Log.d("XXXXXXXXXXXXXXXXXXX", "STEP ONE");
        if (sd.canWrite()) {
            Log.d("XXXXXXXXXXXXXXXXXXX", "STEP TWO");
            String currentDBPath = "/data/com.test.example/databases/example_db";
            String backupDBPath = "/mytest" + "version_two" + ".db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);
            Log.d("XXXXXXXXXXXXXXXXXXX", "STEP THREE");
            if (currentDB.exists()) {
                Log.d("XXXXXXXXXXXXXXXXXXX", "STEP FOUR");
                FileChannel src;
                try {
                    Log.d("XXXXXXXXXXXXXXXXXXX", "STEP FIVE");
                    src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB)
                            .getChannel();
                    try {
                        Log.d("XXXXXXXXXXXXXXXXXXX", "STEP SIX");
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                    } catch (IOException e) {
                        Log.d("XXXXXXXXXXXXXXXXXXX", "STEP ONE DEAD");
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    Log.d("XXXXXXXXXXXXXXXXXXX", "STEP TWO DEAD");
                    e.printStackTrace();
                }
            }
        }else{
            Log.d("XXXXXXXXXXXXXXXXXXX", "STEP DEAD");
        }

    }

Clip of my manifest - I don't know how android:allowBackup="true" got there

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity

On emulator output:

01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP ONE
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP DEAD

On device output:

01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP ONE
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP TWO
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP THREE
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP FOUR
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP FIVE
01-13 14:16:13.090: D/XXXXXXXXXXXXXXXXXXX(30105): STEP SIX
2

1 Answer 1

1
 File sd = new File(Environment.getExternalStorageDirectory().toString());

Well, since Environment.getExternalStorageDirectory() returns a File you do not to create a new instance.You can change it in this way:

 File sd = Environment.getExternalStorageDirectory();
  • sd.mkdirs(); will sure fail, because you have not write permission inside / and /mnt, also the directory pointed by sd should already exits
  • Write permission - WRITE_EXTERNAL_STORAGE : did you add it inside your AndroidManifex file? Probably canWrite() returns false because you forgot to add the permission
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm... I just added it, and I thought it would work, but still nothing. Any ideas?
I still get STEP ONE then STEP DEAD. Urg... any other ideas?
but do you have a sdcard in your mobile?
I'm trying this on an emulator... Is that my problem? oh no. I should've known. I'll go try that right now.
Yeah, it worked on the actual device. Thank you so much for troubleshooting this with me. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.