1

I know its asked many times here, I have gone through many answers, but failed to understand 1 think.

I am trying to show a .jpg image in an ImageView

<ImageView
    android:id="@+id/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/bg" />

and that bg.jpg is 650 k.b in size with 1600 x 1700 WxH resolution placed in drawable folder.

and I faced a out of memory error on few devices.

I was under the impression that the memory size is what matters and 650 k.b is not much. But when I read Displaying Bitmaps Efficiently and Loading Large Bitmaps Efficiently: It looks like the resolution is what matters. And if the configuration is ARGB_8888 according to those pages, my image would be taking 1600 x 1700 x 4 bytes, which is more than 10 m.b

So,

  1. Even when 10 m.b is less than the per app limit of 16 m.b why it gave me this error?

  2. As explained in the above links, if I scale down the Image, there are devices which have resolution of 1200 x 2000 WxH where I would not be able to scale and hence I get the same Error.

I see apps which load multiple full screen images(scrollable) and I am using only 1 :(

what am I missing?

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Try this, just increase your heap memory by adding android:largeHeap="true" this to your manifest file. May be it will work i am not sure weather it will work or not. but this is just one solution that you can try. – InnocentKiller Dec 23 '13 at 07:44
  • Even if it works, I don't want to use this solution as explained [here](http://stackoverflow.com/questions/17054454/androidlargeheap-true-convention#17054902) – Archie.bpgc Dec 23 '13 at 07:48
  • 1
    Getting tired of all largeHeap answers that is always the first answer in an OOM question, that's just hiding the symptoms, not curing them! – Magnus Dec 23 '13 at 11:57

2 Answers2

0

First of all, the per-app heap limit varies from device to device not and is not always set to 16 MB. Your heap will include all kinds of objects like your activities, fragments, views and other objects you have initialized yourself.

So the calculation is not as simple as 16 -10 = 6 MB.

If you want to learn more about how memory analysis, try out this post.

Secondly, a 1600 x 1700 resolution image is gonna be way too big for most android device screens. This is exactly why android has resource qualifiers so that you can provide scaled down versions of your images for different screen resolutions. See docs for more on this.

If you are worried about maintaining the aspect ratio of your image for different resolutions, you can simply set the ImageView.ScaleType property to CENTER_INSIDE.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • had I used resource qualifiers I would end up using bigger images . Because as of now I am considering only `mdpi` and keeping the image in drawable folder, If I consider `hdpi or xhdpi` I might have to use `1.5x 2x image size`. – Archie.bpgc Dec 23 '13 at 09:15
  • Use the size you have right now for xxxhdpi and scale down for the others. – Anup Cowkur Dec 23 '13 at 09:20
  • If there is a device with `1000 x 1000 and xhdpi`, and I want the image to be shown full screen then I have to place a `2000 x 2000 sized image in xhdpi folder` right? – Archie.bpgc Dec 23 '13 at 09:31
  • you can put any size image in any drawable folder you want and android will scale it for you. The only thing is that if you provide the right set of resources, they will look better. I recommend going through the developer docs in detail to understand how resource selection works: http://developer.android.com/guide/practices/screens_support.html – Anup Cowkur Dec 23 '13 at 09:45
0

Use This Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter)

Here dstWidth and dstHeight is int variable.

rupesh
  • 2,865
  • 4
  • 24
  • 50
  • But on bigger screens the `dstWidth` and `dstHeight` would be the same as actual image size. Which again will cause the same error even after putting effort into scaling down. – Archie.bpgc Dec 24 '13 at 03:55