PackageManager.getApplicationIcon()返回默认图标?图标、PackageManager、getApplicationIcon

2023-09-05 08:11:31 作者:古城白衣少年逝

有没有办法来告诉我们,如果绘制对象我碰到getApplicationIcon(回)是默认/内置图标或不?

Is there a way to tell if the Drawable I'm getting back from getApplicationIcon() is a default/built-in icon or not?

例如,我已经安装在我的模拟器多个应用程序。 Adobe Reader的有Adobe提供的图标。 com.android.gesture.builder和样品软键盘,在另一方面,有一个通用的机器人的图标。 getApplicationIcon()为这两个包退换不同BitmapDrawable对象,但在这两个物体运行getBitmap()返回相同的位图对象(android.graphics.Bitmap@401a7df8)。

For example, I have several applications installed on my emulator. "Adobe Reader" has an icon provided by Adobe. "com.android.gesture.builder" and "Sample Soft Keyboard", on the other hand, have a generic Android icon. getApplicationIcon() for those two packages returned different BitmapDrawable objects, but running getBitmap() on those two objects returned the same Bitmap object (android.graphics.Bitmap@401a7df8).

我到目前为止唯一的想法是做类似如何preVIEW R.drawable。*图片并抓住所有的android.R.drawable资源,从中创建可绘,并检查是否位图我从getApplicationIcon回()与其中之一匹配。这是pretty的次优的,虽然。

The only idea I have so far is to do something like How to preview R.drawable.* images and grab all the android.R.drawable resources, create Drawables from them, and check to see if the Bitmap I get back from getApplicationIcon() matches any of them. That's pretty sub-optimal, though.

谢谢!

推荐答案

我刚刚想通了这一点。有一个PackageManager.getDefaultActivityIcon()方法返回一个可绘制对象。如果绘制对象的位图相匹配的应用程序图标绘制对象的位图,那么它的默认图标。

I just figured this out. There's a PackageManager.getDefaultActivityIcon() method that returns a Drawable. If that Drawable's Bitmap matches the application icon Drawable's Bitmap, then it's the default icon.

PackageManager pm = context.getPackageManager();
Drawable icon = pm.getApplicationIcon(apk.package_name);
Drawable default_icon = pm.getDefaultActivityIcon();
if (icon instanceof BitmapDrawable && default_icon instanceof BitmapDrawable) {
    BitmapDrawable icon_bd = (BitmapDrawable)icon;
    Bitmap icon_b = icon_bd.getBitmap();
    BitmapDrawable default_bd = (BitmapDrawable)pm.getDefaultActivityIcon();
    Bitmap default_b = default_bd.getBitmap();
    if (icon_b == default_b) {
        // It's the default icon
    }
}