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
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • This may help: http://stackoverflow.com/questions/4580683/writing-text-file-to-sd-card-fails – Alex Florescu Jan 13 '13 at 09:25
  • Also this : http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder/3551906#3551906. Shouldn't you be specifying a filename as well? It seems that you're only using the directory for creating the file. – Alex Florescu Jan 13 '13 at 09:28

1 Answers1

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
Blackbelt
  • 156,034
  • 29
  • 297
  • 305