1

I'm using android:largeHeap in my Application to get more memory, because I have a large image in a scrollview, otherwise my application would crash with a outofmemory exception.

I have 2 devices, one with Android 4.2 jellybean and another one with Android 2.3 Gingerbread. On the 4.2 the android:largeHeap works fine, but on the 2.3 I can't use it.

Is there a way to get happy with both devices? Thanks for help.

anguish
  • 458
  • 1
  • 7
  • 27

3 Answers3

0

The largeHeap parameter was added in Honeycomb(3.0), so it won't work on lower devices.

However, using the largeHeap parameter is NOT a solution. It must be used as an absolute last resort. It will force Android to terminate any background processes or apps in order to get that extra Heap, which is a big No-No!

You say that you have a large image in a ScrollView. Have you tried using BitmapFactory to scale the image appropriately first?

EDIT: Also, you mentioned that you split the image into three. Don't do that, as each Bitmap will consume extra memory. Instead, keep it as one Bitmap, but perform scaling on the entire Bitmap.

Vinay S Shenoy
  • 4,028
  • 3
  • 31
  • 36
  • I know. What I tried is that I splited the image (over 4096 px width) in 3 parts. I also used the BitmapFactory to set the insamplesize, but the application crashes anyway. In one case it worked, but the scrolling in my scrollview was in slowmo. :/ – anguish Jan 09 '13 at 09:12
  • How much did you set the inSampleSize too? For a 4096px image, even setting it to 8 will not make any noticeable different in quality unless you are running on a High Res AND large(>7") screen.. – Vinay S Shenoy Jan 09 '13 at 09:14
  • If I use 8, I can't read my numbers, because its a bitmap with small numbers on it, so using 3 was the maximum I could use. – anguish Jan 09 '13 at 09:22
  • One trick I've used to handle scaling issues to force scaling based on the memory class of the device. I assume that devices with a lot of memory(48mb or more) also have a larger screen, and so I perform less scaling(In your case, maybe 3).. However, if the memory is low, there's no point in scaling it less, because the fine details of the image won't be visible anyway on a (assumed) low-end device, so you can perform more scaling.. Maybe 8 in your case. – Vinay S Shenoy Jan 09 '13 at 09:45
0

I suppose this is used with the getLargeMemoryClasss() ? That's only available from API 11 // Android 3 // Honeycomb.

As a side-note, I'm not sure you should need it, couldn't you just scale the image?

Nanne
  • 64,065
  • 16
  • 119
  • 163
0

Here is a detailed discussion on stackoverflow on how to efficiently use Bitmaps so that the outofmemory error doesn not occur and you dont have to increase the heapsize of your application.

Community
  • 1
  • 1
Aditya Kushwaha
  • 839
  • 6
  • 12
  • You have linked to your own answer which is a terrible way to eschew problems in Android. I have edited it to point to the accepted answer on that question. – Vinay S Shenoy Jan 09 '13 at 09:41
  • Looks like my edit was too minor to be approved... Anyway, the accepted answer to the question linked is http://stackoverflow.com/a/13363988/1133908 – Vinay S Shenoy Jan 09 '13 at 09:49
  • i meant to take you to that page and thats all. i have been following that question and i know my answer isnt right there. why would i take you there otherwise. Also i edited the link. – Aditya Kushwaha Jan 09 '13 at 09:57
  • 1
    That's good. I have removed the downvote on this answer too now that it's linked to the right one. – Vinay S Shenoy Jan 09 '13 at 12:22