Create a notification Bar with Drawable

2019-08-05 12:30发布

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);

2条回答
时光不老,我们不散
2楼-- · 2019-08-05 12:50

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

查看更多
叼着烟拽天下
3楼-- · 2019-08-05 12:52
 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;
    }
查看更多
登录 后发表回答