0

I am facing a problem regarding how to down scale the bitmap according to device resolution so that device do not give outofmemory exception.

Image is coming form server.

suppose i have bitmap that is good of xxx-hdpi resolution but that bitmap is too big of hdpi resloution device so what i want is to down scale that bitmap according to screen resloution at runtime.

I have seen some of the solutions

How to scale an image down in android

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

but these solution do not work like the way i want

Help and Hint will be Appreciated. Thanks.

Community
  • 1
  • 1
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
  • http://ruchitsharma.blogspot.com/2013/04/android-bitmap-memory-management.html You should provide images for every screen density in your project. Assume you placed your good fitting xxx-hdpi image into xxxhdpi folder. Now you should place this image accordingly downscaled in other folders like xxhdpi, xhdpi, hdpi, mdpi and so on – Stan Apr 22 '15 at 11:55
  • Check my answer http://stackoverflow.com/questions/29430640/outofmemoryerror-while-decoding-and-encoding-base64-string-into-bitmap/29712904#29712904 – Himanshu Agarwal Apr 22 '15 at 11:56
  • Start trying with manual values for factor for downsampling and then design a formula accordingly or try the following libraries to avoid out of memory errors http://square.github.io/picasso/ – Napolean Apr 22 '15 at 11:56
  • I hate thoes guyz who donot give any reason why they down voted the question .... :( – Moubeen Farooq Khan Apr 22 '15 at 11:58
  • Take a look at http://stackoverflow.com/questions/29762168/reducing-image-size/29763180#29763180. From that code you can scale an image by specify the width and height of desire image. So may be it will fit for your need. – Dhaval Patel Apr 22 '15 at 11:59
  • @Stan what if the image coming form server and its size it is good for xxxhdpi and how i scale down that image that will be appropriate for lower densities then xxxhdpi – Moubeen Farooq Khan Apr 22 '15 at 12:00
  • when you got that image from the server, what next do you want to do with it? – pskink Apr 22 '15 at 12:02
  • Then why didn't you mention this in your question and why do you hate people who downvoting your unclear question? BTW you didn't provide the source code with the way you are trying to downscale your image. Bitmaps and OOM is the most common question here on SO. Try to search. – Stan Apr 22 '15 at 12:05
  • Please refer to the following post: http://stackoverflow.com/questions/29768727/outofmemoryerror-in-the-getview-from-custom-baseadapter/29770740#29770740 – Mike Clark Apr 22 '15 at 12:31
  • @pskink when i have the bitmap i want to compress it according to device resolution at runtime. – Moubeen Farooq Khan Apr 22 '15 at 13:35
  • compress? what for? – pskink Apr 22 '15 at 13:37
  • @Stan I don't have a problem when someone down vote a question i was saying that just at-least give a reason . so next time i do not make that kind of mistake. and why i haven't uploaded the source code cos i know how to down scale the image and i have that code but how much down scale an image to appropriate resolution at run time that i dont knw and i think that is separate question, i have searched allot and did not find an answer so that's why i am asking question. – Moubeen Farooq Khan Apr 22 '15 at 13:41
  • @pskink so the device donot throws outofmemory exception. – Moubeen Farooq Khan Apr 22 '15 at 13:42
  • where it throws OOM? – pskink Apr 22 '15 at 13:44
  • Once again: you are saying: "I have seen some of the solutions" which doesn't mean you understand it or you know how to use it really. If I'd ever seen an UFO that doesn't mean I know how it works and who are inside if any. :) – Stan Apr 22 '15 at 19:14

1 Answers1

0

Assume you know how to downscale Bitmap. So you just need to know to which size you should downscale certain image. You could use something like this to determine the device screen size (usually I do it in Application class):

displayMetrics =  getApplicationContext().getResources().getDisplayMetrics();
displayDensity = displayMetrics.densityDpi;
displayHeight = displayMetrics.heightPixels;
displayWidth =  displayMetrics.widthPixels;

Now its up to you to decide what is the max image size your app will allow accorrding to displayHeight and displayWidth values.
If you want to show your images full screen you could set max image side size as displayHeight/2 (actually you should determine the biggest screen side size and for smartphones its usually the height).
If you want to display some previews in a list, you should limit preview's image side size to something like displayWidth/3 (once again you should determine the smallest screen dimension and for smartphones its a width usually).
I have a method like this (to determine a max allowed ListView image side size in px):

public static int getDeviceMaxSideSizeByDensity(){
    int maxSideSize=128;
    if (isHiResDisplay!=null)
    {
        switch (displayDensity) {
            case 120:
                maxSideSize = 64;
                break;
            case 160:
                maxSideSize = 96;
                break;
            case 240:
                maxSideSize = 128;
                break;
            case 320:
                maxSideSize = 256;
                break;
            default:
                maxSideSize = 512;
                break;
        }
    }
    return maxSideSize;
}

you could set other values, bigger or smaller, its up to you once again.
I suggest you to use a lib like Glide (Picasso, Fresco, UIL) to get rid of such minds. Let the lib to decide.

Stan
  • 6,511
  • 8
  • 55
  • 87