Q:

What's wrong with this inSampleSize calculating method for Bitmaps? Out of memory

In my app i'm iterating through URLs of images, decoding and putting them in an ArrayList<Bitmap>.
They may vary greatly in size, so i'm doing a "pre-decode" with the inJustDecodeBounds = true option to calculate the necessary inSampleSize value for the actual decode.
See my method for this below, i hope it's not too hard to understand. Basically i'm aiming for a size similar to the screen size of the device.
for (Element e: posts) {
    if (!e.id().equals("")) {
        //preparing decode
        options = new BitmapFactory.Options();
        input = new URL(e.url).openStream();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(input, null, options);
        input.close();

        //setting inSampleSize if necessary
        int picPixels = options.outWidth * options.outHeight;
        int picScreenRatio = picPixels / screenPixels;
        if (picScreenRatio > 1) {
            int sampleSize = picScreenRatio % 2 == 0 ? picScreenRatio : picScreenRatio + 1;
            options.inSampleSize = sampleSize;
        }

        //actual decode
        input = new URL(e.url).openStream();
        options.inJustDecodeBounds = false;
        Bitmap pic = BitmapFactory.decodeStream(input, null, options);
        input.close();

        picList.add(pic);
    }
}

Code for calculating screenPixels:
Display display = getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int screenPixels = screenSize.x * screenSize.y;

I'm going through ~60 images and around 40 my app crashes with java.lang.OutOfMemoryError.
As i understand, with inJustDecodeBounds = true there's no memory allocated, and if my method is correct (i believe it is), very big images get very big inSampleSizes, so i don't know what could be the problem.
Would appreciate any advice.

A:

No matter that you use inSampleSize you can't easily hold 60 bitmaps as large as your screen in memory.
Assuming a fullhd screen you get about 1920*1080*60*4 bytes.
Thats roughly 500 MB of data. 
You need to somehow change your bitmap loading logic.
In this answer you can find out how much memory you can count with. It differs for various devices.
But always try to make you app as memory efficient as possible.

