Write a private access file to another app's files directory

700 Software picture 700 Software · Nov 30, 2010 · Viewed 12.5k times · Source

The two apps have the same sharedUserId. When I use this code in app1

context.openFileOutput("/data/data/org.me.app2/files/shared-data.dat", MODE_PRIVATE)

I get an exception telling me that the file contains a path separator.

I am trying to write a file from app1 into app2's storage. (I do of course need to make sure that app2's files directory exists first)

Ideally, I would write to a user specific directory instead of an app specific directory, but I do not know if that can be done

Answer

EboMike picture EboMike · Nov 30, 2010

First of all, NEVER use a full path to internal storage like /data/data. Let the operating system give you the path (for example, via Context.getFilesDir() or Environment.getExternalStorageState()). Don't make assumption on where the data is.

Secondly - you already are doing that! Unlike File, Context.openFileOutput already prepends /data/data/[package] to your path, so you don't need to specify that. Just specify the file name.

If you really feel that it's safe and necessary, and if both apps share the same user ID using android:sharedUserId in the manifest, you can get a context of the other app by using Context.createPackageContext() and use CONTEXT_RESTRICTED, then use openFileOutput with only the file name.