Ruslan
Ruslan

Reputation: 9

Export database to Download folder in Android?

I read other posts about export data base file in Android but I cant figure out how create or copy this database on Downloader folder.

I use on my Manifest (permissions):

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

Activity:

I call this function when I press a button but nothing happens (no exceptions and no file copy or created from currentDBPath)

public void exportDatabase() {  
    try {
        File download_folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (download_folder.canWrite()) {
            String currentDBPath = "//data//"+getPackageName()+"//databases//"+"YourWords";
            String backupDBPath = "YourWords";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(download_folder, backupDBPath);

            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) {

    }      
}  

Upvotes: 0

Views: 1166

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007564

NEVER HARDCODE PATHS.

To get a File object pointing at a database, assuming the database is in the default location, use getDatabasePath().

Also, NEVER IGNORE EXCEPTIONS.

Add Log.e("RuslanApp", "Crash in saving database", e); to the catch block, so you can use LogCat to see what may be going wrong.

Upvotes: 1

Related Questions