的apk之间共享原始资源 [英] Share raw resource between apk's

查看:62
本文介绍了的apk之间共享原始资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个APK(App1的),有一个原始资源(RES /生/ mytextfile.txt),我想从另外一个APK(App2的)读取该文本文件。我定义App1的内容提供商和我重写中openFile(乌里,字符串)的方法如下:

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的执行以下操作,试图获得访问mytextfile.txt:

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));

然而,当我读,写出来的BufferedReader的内容,我得到比mytextfile.txt的内容之外的方式了。显然,我做错了什么,所以我倒是AP preciate,如果有人可以点我在正确的方向。 谢谢你,

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,

米尔扎

我打了一些这方面越来越发现文件desriptor回到App2的是指向App1的apk文件。我写出来的BufferedReader的内容保存到一个文件,并做了一个二进制比较App1.apk。这两个文件是相同的。

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.

推荐答案

我有一个APK,从另一个我的APK。

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"

更换com.myapp.shareduserid和com.myapp.process通过自己的值。这将允许对APK到彼此访问数据。

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 );

由您自己包的名字替换com.apk1.app。

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

这篇关于的apk之间共享原始资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆