开发者

Share raw resource between apk's

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜