Hi Iam implementing Push Notifications in Android using GCM. I am trying to set an image for the notification instead of the default app icon. I am able to achieve this using the following code
if(extras.getString("src") != null){
URL url = new URL(extras.getString("src"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap large_icon = BitmapFactory.decodeStream(input);
mBuilder.setLargeIcon(large_icon);
}
Typically the image will be from the web(jpg, png etc) and not something in the device. The above code works but the image is either too big or too small. I would like to know the optimum size or aspect ratio for the bitmap so that I can supply an appropriate image
The other thing here to know is that the base layouts have margins stated on these images, so if you are trying to mimic the behavior seen by the base layouts in a custom layout make sure to do something similar. Checkout notification_template_icon_group.xml for details.
Here I have computed the math to pixels for you (64dp - 12dp):
If I understood your problem perfectly, then the below will help you.
If you have the image already.. then you can set it like
The total one:
You can also get help from this tutorial..
EDIT: To change a bitmap size ..taken from here..
I was having the same problem. This is how I solve it:
First you need to know the max sizes of the notification icon depending of the device resolution. Searching, I found this:
There are 2 approach:
I will explain to you the second one that I implement.
First for get the Image from an URL I use this:
Then I need to know the factor for the new image size. I know that in the server I have the xxhdpi image with a factor of *3.00, I use that for get the global factor:
Now I have to resize the image size and set the new bitmap in the notification icon:
This work for me. I hope you can use it.