Get resources from another apk
Asked Answered
T

2

9

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!

Thomey answered 2/7, 2012 at 5:25 Comment(0)
T
17

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

Thomey answered 2/7, 2012 at 7:50 Comment(0)
S
4

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();
}
Seaway answered 2/7, 2012 at 5:51 Comment(7)
When i tried this out, it was pulling images from my own app :-/. Evert time i ran it, it would go to the next image in the drawable folder.Thomey
yes i am. What it is doing is showing the drawable from my app and not the other app. So if i choose the 6th image down in the list of drawables in the chosen package, it will show the 6th image down in my drawables list instead. Am i just trying to show it in a imageview wrong? Would this work... TEST.setImageDrawable(image);Thomey
yes because it may be possible you have images in your drawable with same recourse id'sBetook
That makes sense. I understand it now, but i am now confused about decoding the image to a bitmap. Would i use decodeResource?Thomey
try as splashImageView.setImageResource(resources.getDrawable(resources.getIdentifier("image1", "drawable", packName)));Betook
Nope, threw an FC saying no resource was found (E/AndroidRuntime(31211): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0)Thomey
Change the resource object in line 9. It should be resources.getDrawable(resID).Lisette

© 2022 - 2024 — McMap. All rights reserved.