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!
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With