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

Maveňツ
  • 1
  • 12
  • 50
  • 89
Suresh Maidaragi
  • 2,173
  • 18
  • 25
  • Use lazy load, please refer to : http://stackoverflow.com/questions/541966/lazy-load-of-images-in-listview – Lei Guo Feb 13 '15 at 08:02
  • i suggest you to use lib for load image rather than code yourself. Some lib are picasso , volley and universal loader – duggu Feb 13 '15 at 08:04
  • What about [inSampleSize](http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize) – Skizo-ozᴉʞS ツ Feb 13 '15 at 08:08

4 Answers4

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

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89
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.

alfdev
  • 1,039
  • 1
  • 10
  • 23
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

Gaskoin
  • 2,469
  • 13
  • 22
-1

when you set bitmap before put this line

System.gc();

Hardik
  • 56
  • 3
  • Java guarantees that GC will occur before this exception is thrown. The suggestion therefore accomplishes precisely nothing. – user207421 Feb 13 '15 at 08:38