从另一个APK获取资源(Get resources from another apk)

2019-06-25 23:36发布

我一直在这个问题挣扎了一整天,都没有成功。 我基本上是试图从其他APK的图像资源。

所以,如果com.example.app有一个叫做image1.png在res文件夹的形象,我想com.example2.app能够访问该资源,并将其放置在一个ImageView的。

我知道你必须使用PackageManager.getResourcesForApplication,但我还有在获得实际的资源是不成功的。

任何帮助都是极好的!

Answer 1:

弄清楚了...

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

检查这里的形式等问题,更多的解释! apk的之间共享原始资源



Answer 2:

试试这个:

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


文章来源: Get resources from another apk