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);
Maybe this helps:
Source: https://stackoverflow.com/a/9390776/1955332
EDIT EDIT EDIT
Try this then: