-6

i am new to android programming i am facing problem in loading bitmaps i have almost 92 bitmaps which i need to load. but the application crashes after 9 or 10 bitmaps.. kindly help me

here is the error Throwing OutOfMemoryError "Failed to allocate a 22050012 byte allocation with 1805056 free bytes and 1762KB until OOM" 09-05 19:49:36.188 2615-2615/com.example.zeeshanahmedawan.yourworriestheirsolutions D/skia: --- allocation failed for scaled bitmap

    mPages = new ArrayList<Bitmap>();
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p1));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p2));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p3));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p4));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p5));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p6));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p7));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p8));
    mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.p9));
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this. – CommonsWare Sep 05 '17 at 14:44
  • Throwing OutOfMemoryError "Failed to allocate a 22050012 byte allocation with 1805056 free bytes and 1762KB until OOM" 09-05 19:49:36.188 2615-2615/com.example.zeeshanahmedawan.yourworriestheirsolutions D/skia: --- allocation failed for scaled bitmap @CommonsWare – Zeeshan Ahmed Awan Sep 05 '17 at 14:50
  • 1
    I'd say you're going about it in the wrong way. Storing inflated Bitmaps in an array is going to use a ton of memory unnecessarily. Why not just keep an array or list of resource IDs and only inflate when required i.e. in the RecyclerView's adapter? – Michael Dodd Sep 05 '17 at 14:54
  • ... and it does surprise you, right? – Phantômaxx Sep 05 '17 at 15:49

3 Answers3

3

If all of your images are similarly sized, you are attempting to allocate ~2028601104 bytes = ~1981055KB = ~1934MB. You have 64MB or less on most Android devices that your app can use.

The image that you failed on is 22050012 bytes in size, when decoded and perhaps scaled. That is equivalent to 2347 x 2347 pixels. Few if any Android devices have screens of that resolution.

So, you need to do several things:

  1. Stop trying to load all of the bitmaps up front. Load them as needed.

  2. Reduce the resolution of the bitmaps.

  3. If you put the bitmaps in any directory other than res/drawable-nodpi/, move them to res/drawable-nodpi/. For example, if you put them in res/drawable/, that is a synonym for res/drawable-mdpi/, indicating that the images are designed for mdpi devices (~160dpi). Android will automatically scale those images up for higher-density devices. If you have images that are large, you cannot afford for Android to do that sort of scaling. res/drawable-nodpi/ tells Android that these images are not tied to a particular density and so should not be scaled this way.

You might also want to reconsider this entire plan. Working with lots of bitmaps — let alone large ones — is complicated even for experienced Android app developers. Memory management is hard. If you are new to Android, perhaps you should consider a project that will be simpler to implement.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You are out of memory.

Your bitmaps are too large to store in the array.

Nathan Headley
  • 152
  • 1
  • 13
  • 1
    The amount of memory in the emulator is meaningless in this case. The OP is attempting to allocate ~2GB, and the per-process heap limit will be far less than that. – CommonsWare Sep 05 '17 at 14:56
  • And there are 92 of them. I am going out on a limb and guessing that they are all similarly sized. – CommonsWare Sep 05 '17 at 14:58
0
  1. If you need just to view the photos, you can use RecyclerView. It keeps in memory only some photos which are on the screen now, and releases photos which have been scrolled out of the screen.

  2. For other purposes you can save all photos to SD-card.

For both cases 1 & 2 use special library (Glide, Picasso,...). Libraries are easy-to-use, they can automatically cache downloaded images on SD-card and have many other benefits. Search for "glide with recyclerview" (for example)

Evgeny Nozdrev
  • 1,530
  • 12
  • 15