1

I'm taking whole screenshot of a WebView and display the bitmap on an ImageView. The bitmap can be 7 screen height. (E.g. 1440x14000 px)

I'm frequently face with

OutOfMemoryError.

I've seen this

This says that load a scaled down version into memory but I don't want to lose image quality. There are the same approaches on the web.

Is there any way to handle OutOfMemoryError without loading scaled down version?

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • The size you describe as a bitmap calculates too 75+ MB, so it might just really be too large to handle. How much mem does the device have? – Aganju Dec 16 '16 at 13:40
  • Android system gives my app 512 MB. – Serdar Büyükkanlı Dec 16 '16 at 13:48
  • Serdar selam :) You can try dividing the large bitmap into sections and displaying them in multiple imageviews. And there are some other good solutions [here](http://stackoverflow.com/questions/6518215/display-huge-images-in-android) – Faruk Yazici Dec 16 '16 at 13:53
  • Selam Faruk, yorumun için teşekkür ederim görünce çok şaşırdım :) I also need to change the bitmap dynamically (b&w effect, adjusting contrast, drawing shapes etc.) on runtime and also there are other components depends on this bitmap. I guess, dividing bitmaps into sections and applying effects to all sections dynamically brings much workload to the system. – Serdar Büyükkanlı Dec 16 '16 at 14:38

2 Answers2

4

The bitmap can be 7 screen height. (E.g. 1440x14000 px)

Note that this means that the user cannot see the whole image at once at full resolution.

I'm frequently face with OutOfMemoryError

On most devices, you will have a very difficult time loading an image that large, as you cannot get a single contiguous memory block that big.

This says that load a scaled down version into memory but I don't want to lose image quality

To some extent, you do not have much of a choice. If you want the user to see the full extent of the picture at once, the image has to be scaled to fit the screen.

Is there any way to handle OutOfMemoryError without loading scaled down version?

There are ImageView replacements that offer pan and zoom. Some of those, such as this one, handle loading in pieces of the image at a time, with whatever scaling is necessary for the current zoom level, to make it more likely that you will be able to show the user the entire image.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm not showing whole bitmap at a time. The bitmap fits the screen by width and the user can scroll up and down with finger to see other parts of the bitmap. So that the user can see the whole image at full resolution by scrolling. – Serdar Büyükkanlı Dec 16 '16 at 13:46
  • 1
    @serdarbuyukkanli: Widgets like the one I link to offer that, and do the memory management to make that possible, loading in pieces of the bitmap at a time, based on the current scroll position. – CommonsWare Dec 16 '16 at 13:47
0

It is not a solution, of course, but I'm also not familiar with your exact needs, so maybe this may help you a little - you can try to play with bitmap options during decoding. Try to use Bitmap.Config.inPreferredConfig as RGB_565 - this will reduce size of your bitmap twice comparing to default ARGB_8888. But, of course, if you use complex images in your web page this may reduce their quality.

Viacheslav
  • 5,443
  • 1
  • 29
  • 36