Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the resource ID of the app icon in my applicaton from an API

I am writing an API that can be included in various APK's. I want to generate a notification with that app's icon, which requires the resource ID. I'm not finding a way to find the resource ID. I don't know the name of the app's icon. Context is passed in to my class in the constructor. Here is what I have so far:

        ApplicationInfo app = mContext.getApplicationInfo();
        packageManager = mContext.getPackageManager();

        Drawable drawable = packageManager.getApplicationIcon(app);
        String packageName = packageManager.getApplicationLabel(app);

        // logical next step, but I don't know the name of the drawable
        int appIconId = mContext.getResources().getIdentifier("???", "drawable", packageName);

Thank you!

like image 636
Janin Avatar asked Dec 04 '13 19:12

Janin


People also ask

What is an application resource file?

In Android, a resource is a localized text string, bitmap, layout, or other small piece of noncode information that your app needs. At build time, all resources get compiled into your application. The resources locates inside res directory of your app.

What is the use of resource ID in Android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

Where do I put an application icon for an application?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Further follow the path to reach the desired folder to add icon (app → res → mipmap). Step 3 - Add you app icon. You can just simply copy and paste the image in mipmap folder.


1 Answers

This method should work for getting the app icon of ANY application, including yours:

String packageName=...; //use getPackageName() in case you wish to use yours
final PackageManager pm=getPackageManager();
final ApplicationInfo applicationInfo=pm.getApplicationInfo(packageName,PackageManager.GET_META_DATA);
final Resources resources=pm.getResourcesForApplication(applicationInfo);
final int appIconResId=applicationInfo.icon;
final Bitmap appIconBitmap=BitmapFactory.decodeResource(resources,appIconResId);
like image 135
android developer Avatar answered Nov 08 '22 06:11

android developer