Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...