Get resources from another apk

我是研究僧i 提交于 2020-01-10 09:04:31

问题


I have been struggling with this issue all day and have had no success. I am basically trying to get an image resource from another apk.

So if com.example.app has an image called image1.png in the res folder, i want com.example2.app to be able to gain access to that resource and place it in an imageview.

I know you have to use PackageManager.getResourcesForApplication, but i have still been unsuccessful in getting the actual resource.

Any help would be awesome!


回答1:


Figured it out...

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

Check here for more explanation form other question! Share raw resource between apk's




回答2:


try this:

final String packName = "com.example.app ";
Resources resources;
try {
    PackageManager manager = getPackageManager();
    resources = manager.getResourcesForApplication(packName);

    int resID = resources.getIdentifier("image1", "drawable", packName);
    Log.d(TAG, "resID = " + resID);
    Drawable image = getResources().getDrawable(resID);
    Log.d(TAG, "resID = " + resID);
}
catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


来源:https://stackoverflow.com/questions/11288147/get-resources-from-another-apk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!