0

I've a root app that does read the database of another app. For this I need to find the path of the database, so I do following:

String foreignAppDatabaseFolderPath = "/data/data/" + foreignPackageName + "/databases";
String foreignAppDatabasePath = foreignAppDatabaseFolderPath + "/" + foreignDbName;

But know I realised, that the user could move an app to the sd card. I anyway change the above code to following:

 String myAppDatabaseFolderPath = context.getDatabasePath("db")
    .getParentFile()
    .getAbsolutePath();
String foreignAppDatabaseFolderPath = myAppDatabaseFolderPath
    .replace(myPackageName, foreignPackageName);
String foreignAppDatabasePath = foreignAppDatabaseFolderPath + "/" + foreignAppDatabaseName;

But how do I find out the path that I should check, if the app was moved to the sd card?

String foreignAppDatabaseSdCardFolderPath = ???;
prom85
  • 16,896
  • 17
  • 122
  • 242
  • you should export a `ContentProvider` if you need to share data between multiple applications, and not to read other apps data directly – pskink Apr 04 '17 at 08:18
  • It's a root app because I'm reading data from another app that's not made by me... So that's no solution for this... It helps people to directly import some data from the other app instead of having to input this data manually. – prom85 Apr 04 '17 at 08:20
  • You have not made clear that the database path would change by installing app on sd card. Please give examples of both paths. – greenapps Apr 04 '17 at 08:23
  • I did not know that either. I can't give an example, I got some user feedback that my app says "database not found" if the app is installed on the sd card and if the app is moved back to the internal storage everything works. So android must store the database of the app on the sd card and I want to know, how I can determine this path (I don't know the path) – prom85 Apr 04 '17 at 08:24
  • Move your own app to sd card and see how database path would change. Does it? – greenapps Apr 04 '17 at 08:26
  • 1
    so maybe `Context#createPackageContext` is your friend? – pskink Apr 04 '17 at 08:27
  • @psink what I do in my app is "hacking" another apps database, if there is a non root method that would be very bad... The suggestion would work with my own apps and with shared package user ids – prom85 Apr 04 '17 at 08:28
  • @greenapps I tried that and it does change on my emulator to something like `/mnt/expand/25cffccd-7ad9-43ba-be10-95cec39634e4/user/0//` So I still need a function to find this path.... – prom85 Apr 04 '17 at 08:44
  • Please compare with getExternalFilesDir() when on sd card. – greenapps Apr 04 '17 at 08:56
  • I tried that already and this does not return the path above but returns `/storage/emulated/0/Android/data//files` on my emulator... – prom85 Apr 04 '17 at 09:01
  • /mnt/expand/25cffccd-7ad9-43ba-be10-95cec39634e4/user/0//. I see such paths too when moving app to SD card. Android 6 and micro SD added to internal storage. Not as portable storage. – greenapps Apr 04 '17 at 09:06
  • Tell the customer to move your app to sd card too and your old code will work. – greenapps Apr 04 '17 at 09:07
  • If I don't add the sd card in this mode, I can't move my app to it... I found this here: http://stackoverflow.com/questions/32413305/how-to-get-sdcardsecondary-storage-path-in-android and I think I can assume that I could check the paths returned and append `"/Android/data//databases/"` – prom85 Apr 04 '17 at 09:07
  • And yes, I can tell the user to move the app, but my app is not moveable to sd card (it has a service in it) and second this is not very user friendly... I will try the paths returned by the method posted in the link above and test them – prom85 Apr 04 '17 at 09:08
  • 1
    so `createPackageContext` throws an exception / returns null? – pskink Apr 04 '17 at 09:18
  • Sorry @pskink I did not get the idea behind your suggestion, I'll test this, this may really work. Of course I can't acces the database with this context, but for retrieving the path it may be useful – prom85 Apr 04 '17 at 09:26
  • @pskink thanks, this really seems to work. In my emulator this works perfectly – prom85 Apr 04 '17 at 09:46
  • good, if the `Context` is returned successfully, why wouldn't it work? – pskink Apr 04 '17 at 09:51
  • I had in mind to access the database or code with this context that's why I thought it won't work – prom85 Apr 04 '17 at 09:53

0 Answers0