1

I am trying to create a custom Notification bar using a Drawable Object, but it don´t receive Drawable in the parameter, only Bitmap.

The Problem:

I need to get the icon of another application to set in my Notification. I tried use the code below:

getPackageManager().getApplicationIcon(packageName);//Return a Drawable Object

//Problem
Notification.Builder builder = new Notification.Builder(context);
builder.setLargeIcon(Bitmap); //I don´t have this one, only a Drawable object.

As you can see. How I can use this Object to put in my Notification bar ?

Result expected:

Create a Notification Bar with the icon of another application, name and the Action to this one.

SOLUTION:

Thanks, pietmau and kadrei.

Please use this code below:

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

and complete with this one:

Notification.Builder builder = new Notification.Builder(context);

Drawable icon = getPackageManager().getApplicationIcon(
                packageName);
Bitmap bitmapIcon = drawableToBitmap(icon);

builder.setIcon(android.R.drawable.stat_sys_download_done).//It´s necessary put a resource here. If you don´t put any resource, then the Notification Bar is not show.
setLargeIcon(bitmapIcon);

Notification notification = builder.getNotification();
notificationManager.notify(yourId, notification);
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
thiagolsilva
  • 439
  • 4
  • 11

2 Answers2

1
 Bitmap= BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.drawable);

EDIT EDIT EDIT

Try this then:

    public static Bitmap drawableToBitmap (Drawable drawable) {
       if (drawable instanceof BitmapDrawable) {
           return ((BitmapDrawable)drawable).getBitmap();
       }

       int width = drawable.getIntrinsicWidth();
       width = width > 0 ? width : 1;
       int height = drawable.getIntrinsicHeight();
       height = height > 0 ? height : 1;

       Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
       Canvas canvas = new Canvas(bitmap); 
       drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
       drawable.draw(canvas);

       return bitmap;
    }
Dumbo
  • 13,555
  • 54
  • 184
  • 288
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • pietmau, Hello, Thanks for your arswers. I don´t have this Drawable in my project.I am getting the icon of another application using the PackageManager. Do you have another sugestion ? – thiagolsilva Feb 26 '13 at 19:34
  • 1
    @thiagolsilva well, if you like one of the answers, pick up one and accept it by clicking the green thick on the right ;-) – Lisa Anne Feb 26 '13 at 20:04
1

Maybe this helps:

public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable)drawable).getBitmap();
}

int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); 
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}

Source: https://stackoverflow.com/a/9390776/1955332

Community
  • 1
  • 1
kadrei
  • 11
  • 4
  • 1
    you can also mark this answer as the right one by clicking on the green thing under the votes, so other Users might see more easily that this solves the problem. – kadrei Feb 27 '13 at 10:37