Get resources from another apk || Share raw resource between apk's

Issue1:

final String packName = "com.example2.app";
String mDrawableName = "app_icon";

try {
    PackageManager manager = getPackageManager();
    Resources mApk1Resources = manager.getResourcesForApplication(packName);

    int mDrawableResID = mApk1Resources.getIdentifier(mDrawableName, "drawable",packName);

    Drawable myDrawable = mApk1Resources.getDrawable( mDrawableResID );

    if( myDrawable != null )
        TEST.setBackgroundDrawable(myDrawable );

}
catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Issue2:
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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容