简体   繁体   English

从另一个APK获取资源

[英]Get resources from another apk

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. 我基本上是试图从另一个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. 因此,如果com.example.app在res文件夹中有一个名为image1.png的图像,我希望com.example2.app能够访问该资源并将其放置在imageview中。

I know you have to use PackageManager.getResourcesForApplication, but i have still been unsuccessful in getting the actual resource. 我知道您必须使用PackageManager.getResourcesForApplication,但是我仍然无法获得实际的资源。

Any help would be awesome! 任何帮助都是极好的!

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 在APK之间共享原始资源

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM