5

Hello I am looking for the way to set the bitmap which are not in res directory. Actually I am getting that icon from the URL and want to set it in the notification area.

Here I am doing but it is only set the icon which are in res directory.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_stat_logo)
    .setContentTitle(Util.notificationTitle)
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notificationMessage))
    .setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setContentText(notificationMessage);

Any idea to set the fetch bitmap from URL and set that icon as Notification icon ?

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • What if you fetched an downloaded it into your local space and then referenced it that way with its absolute path? Then it is downloaded and in a sense, cached. – Fallenreaper May 23 '14 at 19:20
  • 1
    @Fallenreaper but this method `setSmallIcon` only accept the int in the method argument not any path – N Sharma May 23 '14 at 19:21

4 Answers4

9

Already answered here: https://stackoverflow.com/a/16055373/1071594

Summary: It's not possible to set a custom small icon, but from API level 11 you can use setLargeIcon() after you have downloaded your image and converted it to a Bitmap.

[edit] There is another solution: If you create a completely custom notification with its own view, you could put anything inside that view, including downloaded images.

Community
  • 1
  • 1
ByteWelder
  • 5,464
  • 1
  • 38
  • 45
6

In API level 23 , Android has introduced new method to setSmallIcon using bitmap downloaded from url.

    notificationBuilder.setSmallIcon(Icon.createWithBitmap(yourDownloadedBitmap));
rafa
  • 1,319
  • 8
  • 20
  • 5
    Anyone has a good way to use this in combination with NotificationCompat ( means icon for 23+ and res-id fallback for x<23 ) ? – ligi Jul 31 '16 at 15:36
  • 4
    This not works, setSmallIcon(int) cannot be applied to android.graphics.drawable.Icon – vinidog Dec 23 '17 at 14:59
  • Instead of using Icon.createWithBitmap I used IconCompat.createWithBitmap since one of the setSmallIcon method needs an IconCompat object. – Juan P. May 08 '23 at 14:31
4

I think you can't use directly the URL, but you can use the following statement, but only if you use a large icon.

This statement converts a URL into a BitMap:

Bitmap bitmap = getBitmapFromURL("Your URL");


public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Now, in your notification builder you can use the following code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setLargeIcon(bitmap)
    .setContentTitle(Util.notificationTitle)
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notificationMessage))
    .setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setContentText(notificationMessage);

Don't forget the permissions in your Manifest:

<uses-permission android:name="android.permission.INTERNET" />
-2

The Simple way to set up a custom png image to Notifications is by create a drawable folder in app/src/main/res folder and paste the image to that folder then you can access that image like this

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.imagename);