1

This is the method i wrote to get image from url to bitmap, and i have made to run whenever i scroll down the main view using Handler

public void setimage(final ImageView imageview, final String urll,final int postion)
{

new Thread(new Runnable() 
 {

    @Override
    public void run() 
    {
        URL url = null;
        try {
            url = new URL(urll);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            final Bitmap myBitmap = BitmapFactory.decodeStream(input);

            handler.post(new Runnable() 
            {
                @Override
                public void run() {

                    imageview.setImageBitmap(Bitmap.createScaledBitmap(myBitmap, 160, 140, true));
                }
            });

            web.get(postion).setImage(myBitmap);

        }
        catch (IOException e) 
        {
            e.printStackTrace();

        }

    }

}).start();

}

This is the exception i am getting in Custom adapter whenever i scroll down to view more images in logcat

java.lang.OutOfMemoryError android.graphics.BitmapFactory.nativeDecodeStream(Native Method)

Please let me know where i am doing wrong

3
  • Use lazy load, please refer to : stackoverflow.com/questions/541966/… Commented Feb 13, 2015 at 8:02
  • i suggest you to use lib for load image rather than code yourself. Some lib are picasso , volley and universal loader Commented Feb 13, 2015 at 8:04
  • What about inSampleSize Commented Feb 13, 2015 at 8:08

4 Answers 4

2

You can request to use more by using

android:largeHeap="true"

in the manifest.

Reference: How to solve java.lang.OutOfMemoryError trouble in Android

Sign up to request clarification or add additional context in comments.

Comments

1

You could use the decodeStream's overload that accept, in the third parameter, a BitmapOptions to scale down size and image's quality.

Have a look at this

Hope this can help you.

Comments

1

Both answers are really not too good. Large heap can mean anything. On some devices it will be ok, on some it won't. Android does not provide any information about how big this heap will be. If your bitmap is really big, you should download it not to the ram memory, but to the flash (some file). And than immediately read it scaled down. In this method you also has a cache implementation for free :)

Please check this article http://developer.android.com/training/displaying-bitmaps/index.html

Comments

-1

when you set bitmap before put this line

System.gc();

1 Comment

Java guarantees that GC will occur before this exception is thrown. The suggestion therefore accomplishes precisely nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.