Getting resources of another Application
Asked Answered
C

5

11

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?

Edit:

You can also designate an Android project as a library project, which allows it to be shared with other projects that depend on it. Once an Android project is designated as a library project, it cannot be installed onto a device.

Does it mean That I can not use my library on android market? My aim is to use default resources of Application A, but also if someone what he can download Application B and use it resources.

Cordeiro answered 26/8, 2011 at 13:31 Comment(2)
Why would that be a good idea?Vibrant
Would't that make your code unreadable ?Antipersonnel
S
23

If you are the developer of both applications then the right approach would be, as othes noted, to create an Android library project and share it between apps.

Nevertheles, you can still access resources from another app, even if this is not your app:

Resources res = context.getPackageManager().getResourcesForApplication("com.example.foo")
Sanfordsanfourd answered 26/8, 2011 at 14:4 Comment(2)
Resources are assigned IDs which changed every compilation. How can I access them via name? See also #4593546Stopped
Try Resources.html#getIdentifier(..): (developer.android.com/reference/android/content/res/…Sanfordsanfourd
M
5

If both apps are your own and you want to bundle these resources at build-time:

Create an Android Library Project, put all the needed resources into it and reference it from both your apps.


Edit to your edit: You can use the library in the android market. You just can not use it alone (compile it). You have to build an app that uses the library. When you build that app, all the needed content gets basically grabbed from the library and copied to your apk. Which you can put on the market as usual.

This does not work when you want to access resources from an app that gets downloaded onto the device at runtime. A library project bundles all resources when you build the app that uses it. Peter Knego's response is the correct one when accessing at runtime.

Maggi answered 26/8, 2011 at 13:42 Comment(1)
Thanks, Yes I want to use it at runtime, I will Use Peter Knego way.Anticathode
P
1

It is possible if those both application are yours. Or you can used any Android Library for both these application. If you want you also create your own Android Library for this kind of work. Thnx.

Pileous answered 26/8, 2011 at 13:51 Comment(0)
A
0

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.

Ardellaardelle answered 11/1, 2015 at 23:24 Comment(1)
Could the -1 be explained pleaseArdellaardelle
E
0

For example, let's get the string-array with the ID of entryvalues_font_size from the package com.android.settings

String[] enries = null;
try {
    // You can get PackageManager from the Context
    Resources res = getPackageManager().getResourcesForApplication("com.android.settings");
    int resId = res.getIdentifier("entryvalues_font_size", "array", "com.android.settings");
    if (resId != 0) {
        enries = res.getStringArray(resId);
    }
}
catch (NameNotFoundException e) {
    e.printStackTrace();
}
Eleanore answered 8/5, 2017 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.