Getting resources of another Application

后端 未结 5 1340
别跟我提以往
别跟我提以往 2020-12-03 05:43

Assume I\'ve got 2 Application A and B.

I want to access resources (drawables, images, strings) of an B application from A application. How would I do that?

<
5条回答
  •  孤城傲影
    2020-12-03 06:43

    Peter's answer above is great, but I'd like to elaborate a little more...

    When you control appA and appB and want to get a drawable from appB, while in appA, then do the following:

    1. Get the identifier of the drawable when in appB (for example, R.drawable.iconInAppB)
    2. Then, in appA

      Resources res = context.getPackageManager().getResourcesForApplication("com.example.appB");
      
          Drawable d = res.getDrawable(integer);
      //integer was what appB assigned to R.drawable.iconInAppB
      

    *Note: The integer id assigned to R.drawable.iconInAppB will change over time so don't rely on hardcoding. I save the key-value pairs (String resourceName, int resourceId) in a database in appB and access them using a content provider.

提交回复
热议问题