Issue
I prepared images for my android app. I put images which have different sizes to drawable folders because of different device sizes. Now i want to get these from url. I want to get the image that is most appropriate for the device's size. How should I do this? Is there any suggest?
Solution
From the api, you need to send URI to images of all sizes, then in android you need to download the image that suits the device's dpi value, the code for that is like this:
You can get the density value of your device like this
float density = Resources.getSystem().getDisplayMetrics().density * 160f;
or
float density = context.getResources().getDisplayMetrics().density * 160f;
Then you do a series of if else statements and download the appropriate image from your sever.
if (density == DisplayMetrics.DENSITY_HIGH) {
return downloadLargeImage();
} else if (density == DisplayMetrics.DENSITY_MEDIUM) {
return downloadMediumImage();
} else if (density == DisplayMetrics.DENSITY_LOW) {
return downloadSmallImage();
} else {
return downloadMediumImage();
}
There are more density values which you can run the if else loop on, I usually just take care of these 3 cases
Or there is another way too, what you can do is send the devices density value to the server along with the get request for the image, and do this if else calculation on the server and return the appropriate image file.
Both methods are effectively same, but the 2nd method is semantically wrong, your server shouldn't be the one deciding which image to send, but instead your device determining which image size to download
Answered By - Bhargav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.