Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing image in bigpicture style notification

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?

like image 978
Jarvis Avatar asked Jun 23 '15 05:06

Jarvis


1 Answers

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();
like image 83
Wajdan Ali Avatar answered Oct 31 '22 20:10

Wajdan Ali