3

i need set the small Android notification icon through PNG from a external URL instead of using mipmap.

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        notificationBuilder.setSmallIcon(R.mipmap.ic_notification);
        notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));

    }

Need to pass a external PNG image instead of using: R.mipmap.ic_notification

Does anyone know if it is possible?

vinidog
  • 372
  • 2
  • 7
  • 19
  • i don't think its possible with Builder directly . it only takes `int` argument which is `id` of resource . – ADM Dec 23 '17 at 04:19

1 Answers1

3

If you read the developer documents specific to Notification.Builder you will see that setSmallIcon(int icon) needs a A resource ID in the application's package of the drawable to use.

Downloading an image, converting to a bitmap and then setting it to the setSmallIcon(int resId) is still going to give you an error.

Even if you were to convert the Bitmap to a Drawable like this for instance:

Drawable d = new BitmapDrawable(getResources(), bmpFinal);

it is still going to give you an error because that Drawable does not exist in your application package.

The only possible solution is to use a Drawable resource that exists in your package and set it to the setSmallIcon() method. Typical usage:

builder.setSmallIcon(R.drawable.ic_launcher);

Alternatively, the setLargeIcon (Bitmap icon) requires a Bitmap instance. Without having to make any additional changes in your current code (since you already have a Bitmap), you can use that as it is, if it fits your requirement.

If not, you pretty much have to use a Drawable resource that is already present in one of the drawable folders.

ADM
  • 20,406
  • 11
  • 52
  • 83