Natthawoot
Natthawoot

Reputation: 25

OutOfMemory when load image to bitmap from URL

I load image from url. it' ok, but when long time it error outofmemory: bitmap size exceeds vm budget. here my code

    BitmapFactory.Options bmOptions;
    bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    //from web
    try {
        Bitmap bitmap=null;
        InputStream is=new URL(url).openStream();
        BitmapFactory.decodeStream(is, null, bmOptions);
        is.close();
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = 1;
        is = new URL(url).openStream();
        bitmap = BitmapFactory.decodeStream(is, null, o2);
        is.close();
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }

help me please!!

Upvotes: 0

Views: 1263

Answers (4)

Jokahero
Jokahero

Reputation: 1074

Try using the sampleSize of BitmapFactory.Options, this will reduce the size in memory of your image (and the quality)

But if your image is really too big, I think that's there no miracle solution, the image is simply too big ...

Upvotes: 2

Mark Bakker
Mark Bakker

Reputation: 1348

Just a idea, it could be this piece of code throws sometime a exception. This will causes that the InputStream will not release rthe asigned resources (memory leak).

if you close the Input stream in a finaly block this issue should be solved

Upvotes: 0

Stuti
Stuti

Reputation: 1638

Try with any other image. As this image is exceding the size of ur folder.

R u using Emulator to test it?

Upvotes: 0

inazaruk
inazaruk

Reputation: 74780

Your problem is exactly what stated in error message: your image is too big to be loaded into memory.

Upvotes: 1

Related Questions