3

I am working on a rooted device. I have connected with the adb shell from my pc and I can verify that the other database exists and I can query its tables.

However in the my java code when I try to open the database I get error

android.database.sqlite.SQLiteException: unable to open database file

I am trying to open the database like this:

SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READONLY);

The path is something like /data/data/com.xxx.xxx/databases/xx.db

Am I supposed to read the databases of other applications like this or there is another way?

UPDATE: I also tried making the system app as adviced in this thread Push my apk to /system/app

I think it works because the app cannot be uninstalled after that from the applications manager like the ordinary apps so I think it is a system app. But still I cannot access the database though I am sure the paths and permissions are ok.

Community
  • 1
  • 1
gop
  • 2,150
  • 5
  • 26
  • 54

3 Answers3

9

Android OS by default does not allow applications to access each others private folders. To be able to read files that are in another applications folder either:

1) you need to have both applications installed using same user id. Add this to both manifests:

android:sharedUserId="com.xx.xx"

2) If other app is a 3rd party app then your only choice is to install your app as system application. To do that you need a rooted phone and you need to mount the system folder. And then you need to put your application into /system/app/ folder and reboot the phone. It should be installed automatically during the boot.

Caner
  • 57,267
  • 35
  • 174
  • 180
  • Thank you for the response @Caner what I did was to download an app from the store named Root Browser and copied the .apk to the /system/app then I rebooted, the app was installed indeed but the result is the same. I double checked to see if the db path is the same. I am using a rooted HTC Desire HD with Cyanogenmod. Also through the Root Browser app I gave all permissions to the apk in the /system/app folder. – gop Jun 28 '12 at 13:17
  • open the shell and do `su` and then try `chown root.root /system/app/your.apk` – Caner Jun 28 '12 at 13:53
  • unfortunately this didn't help either, the Root Browser app shows correctly that the owner of the apk is root but this doesn't change anything. Interesting thing is that once I copy the .apk to the /system/app folder the app automatically is installed without restart needed(though I tried restarting the device too). Here are the few lines of the stacktrace which are interesting: sqlite returned: error code = 14, msg = cannot open file at line 27205 of [42537b6056] sqlite3_open_v2("/data/data/com.facebook.katana/databases/fb.db", &handle, 1, NULL) failed – gop Jun 28 '12 at 17:36
  • 1
    i dont know then :( maybe this could help: http://stackoverflow.com/questions/8021578/how-to-run-shell-commands-with-root-privileges-in-java you can try to copy the database to some folder you can access – Caner Jun 29 '12 at 07:40
  • 2
    finally this is what I ended doing and it works String[] hin = { "su", "-c","cp /data/data/com.xxx.xxx/databases/xx.db /sdcard/xx.db" }; Runtime.getRuntime().exec(hin); From there I could access the .db. Now I have another question but I will create a new thread for it. Thanks! – gop Jun 29 '12 at 18:42
1

I would assume that the permissions on the database files are set such that your application has no acess. Even if your device is rooted it doesn't mean that your application is running as root.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks @David Wasser but if I use chown on the .apk doesn't this make the app run as root? If it is running as root is it still possible that I have no access to the database? – gop Jun 28 '12 at 20:27
  • Good question. I don't play much with rooted phones so I guess I can't help you here. Sorry. You should be able to see if your app is running as root by using `adb shell` and a `ps` command which should show you the userid that each process runs as. – David Wasser Jun 28 '12 at 20:45
1

This is because the app needs root, and needs to change the permissions of the database you are trying to access so that you can actually access it. What you will need to do is, as root, change the permissions of the database so that everyone can access it, do what you would like on the database, close the database and change the permissions back.

This does circumvent security of android, so proceed at your own risk. You can find a code sample at:

http://rratmansky.wordpress.com/?p=259&preview=true

user1883083
  • 841
  • 1
  • 8
  • 10