Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 772
700 Software Avatar asked Nov 30 '10 21:11

700 Software


3 Answers

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.

like image 176
EboMike Avatar answered Sep 25 '22 19:09

EboMike


Open a FileOutputStream of the needed file, relative to this path:

String filePath = getPackageManager().
    getPackageInfo("com.your2ndApp.package", 0).
    applicationInfo.dataDir;
like image 33
ognian Avatar answered Sep 22 '22 19:09

ognian


Since this is months old I assume you've already solved your problem, but I'll contribute anyway.

Sharing data between apps is what ContentProviders are for. Assuming that you know how to write a ContentProvider and access it, you can access files via ParcelFileDescriptor, which includes constants for the mode in which you create the files.

What you need now is to limit access so that not everybody can read the files through the content provider, and you do that via android permissions. In the manifest of one your apps, the one that will host the files and the content provider, write something like this:

<permission android:name="com.example.android.provider.ACCESS" android:protectionLevel="signature"/>

and in both apps add this:

<uses-permission android:name="com.example.android.provider.ACCESS" /> 

by using protectionLevel="signature", only apps signed by you can access your content provider, and thus your files.

like image 33
palako Avatar answered Sep 22 '22 19:09

palako