Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a database backup to SDCard on Android

Tags:

android

I am using the below code to write a backup copy to SDCard and I get

java.io.IOException: Parent directory of file is not writable: /sdcard/mydbfile.db

private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {         private final ProgressDialog dialog = new ProgressDialog(ctx);          // can use UI thread here         protected void onPreExecute() {            this.dialog.setMessage("Exporting database...");            this.dialog.show();         }          // automatically done on worker thread (separate from UI thread)         protected Boolean doInBackground(final String... args) {             File dbFile =                     new File(Environment.getDataDirectory() + "/data/com.mypkg/databases/mydbfile.db");             File exportDir = new File(Environment.getExternalStorageDirectory(), "");            if (!exportDir.exists()) {               exportDir.mkdirs();            }            File file = new File(exportDir, dbFile.getName());             try {               file.createNewFile();               this.copyFile(dbFile, file);               return true;            } catch (IOException e) {               Log.e("mypck", e.getMessage(), e);               return false;            }         }          // can use UI thread here         protected void onPostExecute(final Boolean success) {            if (this.dialog.isShowing()) {               this.dialog.dismiss();            }            if (success) {               Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();            } else {               Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();            }         }          void copyFile(File src, File dst) throws IOException {            FileChannel inChannel = new FileInputStream(src).getChannel();            FileChannel outChannel = new FileOutputStream(dst).getChannel();            try {               inChannel.transferTo(0, inChannel.size(), outChannel);            } finally {               if (inChannel != null)                  inChannel.close();               if (outChannel != null)                  outChannel.close();            }         }       } 
like image 905
Pentium10 Avatar asked May 11 '10 20:05

Pentium10


People also ask

Where is the DB file stored in Android?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.

Is there a database on Android?

database. Contains classes to explore data returned through a content provider. If you need to manage data in a private database, use the android.


1 Answers

Do you have permissions defined in manifest ?

<uses-permission     android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
like image 78
Alex Volovoy Avatar answered Sep 21 '22 03:09

Alex Volovoy