3

I am trying to export an sqlite database to the sdcard. When I test the application (both on real device and on emulator) I get an error regarding the path of my database. When I check from the DDMS the path of the database it seems to be the one that is printed as not found.

File dbFile =
            new File(Environment.getDataDirectory() + "/data/"+c.getPackageName()+"/databases/myDB.db");

AndroidManifest.xml

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

I used the code found here: Making a database backup to SDCard on Android

Community
  • 1
  • 1
JustCurious
  • 1,848
  • 3
  • 30
  • 57
  • why don't you use `SQLiteOpenHelper` ? http://developer.android.com/guide/topics/data/data-storage.html#db – Marek Sebera Jan 25 '12 at 23:41
  • 1
    I had a similar problem (or two) with an app I was programming. On the device the trouble I was having (with a simple file, actually) was that I hadn't declared I/O in the manifest. In my operating system (Windows 7) the trouble was that Eclipse didn't have permissions to create/read files (I solved it by running Eclipse with Admin privileges) – DigCamara Jan 25 '12 at 23:43
  • my database actually extends SQLiteHelper – JustCurious Jan 25 '12 at 23:47
  • I have the permission shown above. isnt it enough? – JustCurious Jan 25 '12 at 23:49
  • Even running Eclipse with Admin Privileges, does not find the file – JustCurious Jan 25 '12 at 23:56
  • check this link http://stackoverflow.com/questions/8141330/android-can-not-find-the-file-although-it-exists-in-the-data-folder – Rakhita Jan 27 '12 at 04:58

2 Answers2

1

Firstly, don't use hardcoded paths for anything in Android - they're not guaranteed to be the same across all devices.

Try using getDatabasePath("myDB.db").getAbsolutePath();

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • this actually produces the wrong path... 01-26 00:00:59.744: E/mypck(403): /data/data/com.android.myapp/databases/myDB.db/myDB.db (No such file or directory) – JustCurious Jan 26 '12 at 00:28
  • @JustCurious: Sorry, my bad. Don't add the database name onto the end - I was thinking of a different API. Look at my answer again, I've edited the command to use. – Squonk Jan 26 '12 at 02:13
  • for the emulator, it worked with just altering the db name to "myDB" without the extension .db – JustCurious Jan 26 '12 at 09:55
0

This problem is commonly caused by incorrectly starting an activity. Activities (except the first) have to be called by intents. Starting an activity using something like

Activity a = new ActivityExample();

will cause this error to be thrown on all file system access calls

ghostbust555
  • 2,040
  • 16
  • 29