I want to display an image in my notification using builder style big picture (and not using the custom view). The image is coming cropped always. How should I display the proper image for different devices depending on the screen ratios?
I have searched and I have got that we need to display the notification by scaling the image depending on the screen width but I am not able to implement that and getting too confused with ppi, dpi and all other formats provided for image. Please can anyone provide me with the code to convert the bitmap to be shown in proper format on the different devices depending on the screen size?
This is how you scale your received bitmap where 2nd and 3rd arguments are width and height respectively
Bitmap b = Bitmap.createScaledBitmap(receivedBitmap, 120, 120, false);
Incase you want to scale by maintaining the aspect ratio according to your width, then do this
public Bitmap getResizedBitmap(Bitmap bm, int width ) {
float aspectRatio = bm.getWidth() /
(float) bm.getHeight();
height = Math.round(width / aspectRatio);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
bm, width, height, false);
return resizedBitmap;
}
and to generate the notification with scaled bitmap , use this
NotificationManager notificationManager = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(mContext)
.setContentTitle("set title")
.setContentText("Swipe Down to View")
.setSmallIcon(mContext.getApplicationInfo().icon)
.setLargeIcon(receivedBitmap)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new Notification.BigPictureStyle()
.bigPicture(b))
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0])
.build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With