Share raw resource between apk's
Asked Answered
B

2

2

I have an apk (App1) that has a raw resource (res/raw/mytextfile.txt) and I would like to read that text file from another apk (App2). I define a content provider in App1 and I override the openFile(Uri, String) method as follows:

Resources res = getContext().getResources();
return res.openRawResourceFd(R.raw.mytextfile).getParcelFileDescriptor();

App2 does the following to try to get access to mytextfile.txt:

ContentResolver cr = getApplicationContext().getContentResolver();
ParcelFileDescriptor fd = cr.openFileDescriptor(uri, "r");
InputStream is = new FileInputStream(fd.getFileDescriptor());
BufferedReader bis = new BufferedReader(new InputStreamReader(is));

However, when I read and write out the contents of the BufferedReader, I get way more than just the contents of mytextfile.txt. Clearly I'm doing something wrong so I'd appreciate it if somebody could point me in the right direction. Thanks,

Mirza


I played with this some more and found that the file desriptor returned to App2 is pointing to the App1 apk file. I wrote out the contents of the BufferedReader to a file and did a binary comparison to App1.apk. The two files were identical.

Begga answered 17/11, 2010 at 5:29 Comment(0)
A
3

I have an APK that reads drawables from another of my APK.

To do so, I have set the following fields in the manifest file for both projects:

android:sharedUserId="com.myapp.shareduserid"
android:process="com.myapp.process"

Replace the com.myapp.shareduserid and com.myapp.process by your own values. This will allow both the APK to access data from each other.

Once that is done you can use something similar to the sample I am providing here. Most likely you can do the same with raw. The example here uses drawables:

mApk1Context = createPackageContext("com.apk1.app",Context.CONTEXT_IGNORE_SECURITY);
mApk1Resources = mApk1Context.getResources();
mDrawableResID = mApk1Resources.getIdentifier("mypng", "drawable","com.apk1.app");
Drawable myDrawable = mAndromedaAddonResources.getDrawable( mDrawableResID );
if( myDrawable != null )
    ((ImageView) findViewById(R.id.mywindow)).setBackgroundDrawable(myDrawable );

Replace com.apk1.app by your own package name.

Amigo answered 23/2, 2012 at 20:43 Comment(0)
S
-1

there is no way to do that as Android Library Projects cannot have raw assets.

Sigma answered 17/11, 2010 at 9:5 Comment(3)
Fred, neither project is setup as an Android Library Project. They are both just regular application projects.Begga
you can only put raw assets in apks not jars hence the Android Library Project mention..answer still stand as not possible.Sigma
Fred, thank you for taking the time to respond. However, I am not sure I understand the mention of library projects. The projects in questions are both application projects. They are separate standalone applications that both contain raw resources which I can read from the respective app. So, App1 has res/raw/data1.txt and App2 has res/raw/data2.txt and both apps can read their respective raw resources. Furthermore, in the initial setup, I do get the contents of the file I request. The problem is that I get other resources as well and I have no way of separating them out.Begga

© 2022 - 2024 — McMap. All rights reserved.