0

I want to shows the images in imageview from an array list,the array containing a list of URL like

private String imageUrls[] = {"http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                            "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                            "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png"
                            };

When i go to next image it shows it shows the OutOfMemoryException.

Please help

I used the code as follows

protected Bitmap doInBackground(String... urls) {
              Bitmap bitmap = null;
              try {



          String url = urls[0];

                InputStream in = null;
                try {
                    in = new java.net.URL(url).openStream();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                bitmap = BitmapFactory.decodeStream(in);


              } catch (OutOfMemoryError e) {
                  Log.e("MyApp", e.getMessage());
              }
              return bitmap;  
          }

          protected void onPostExecute(final Bitmap result) {
              handler=new Handler();
              handler.post(new Runnable() { 
                    @Override 
                    public void run() { 

                         bmImage.setImageBitmap(result);

                    } 
                }); 

          }
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73

3 Answers3

0

There's a dedicated page on the android developer website about this topic: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.

Philippe A
  • 1,252
  • 9
  • 22
0

Bitmaps in Android can be a minefield for errors like this. You would save a large amount of time by using a well structured and well tested library to perform this task for you.

Picasso for example, is extremely good.

In your case:

imageUrls[] = {"http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                            "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                            "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png"
                            };

Picasso.with(context).load(imageUrls[index]).into(bmImage);
Knossos
  • 15,802
  • 10
  • 54
  • 91
0

I had similar usecase where I had to download bunch of pics from external URL and show them in single activity. As explained by @Aashvi's link the comments, images take up 4*imageheight*imageWidth about of bytes.. So I started loading thumbnails of it. (of maximum height 300 along with maintaining the aspect ratio). I am pasting here code from my project in which asynctask gets images and makes smaller bitmap of it and sets on imageview and references used.

   protected Bitmap doInBackground(String... urls) {

     Bitmap mPic = null;
    try {

        mPic = CommonUtils.getThumbnailFromUrl(urldisplay);
    } catch (OutOfMemoryError e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

}

protected void onPostExecute(Bitmap result) {
        if (result != null) {
            bmImage.setImageBitmap(result);
        }
 }

Here is the implementation of getThumbNailFromUrl

public static Bitmap getThumbnailFromUrl(String url) {
    Bitmap bitmap = null;
    InputStream input;
    try {
        input = new java.net.URL(url).openStream();

        if (input != null) {
            BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
            onlyBoundsOptions.inJustDecodeBounds = true;
            onlyBoundsOptions.inDither = true;//optional
            onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
            BitmapFactory.decodeStream(input, null, onlyBoundsOptions);

            input.close();
            if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
                return null;

            int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;

            double ratio = (originalSize > 300) ? (originalSize / 300) : 1.0;

            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
            bitmapOptions.inDither = true;//optional
            bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
            input = new java.net.URL(url).openStream();
            bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
            assert input != null;
            input.close();
        }

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

}

This code has been taken from following stackoverflow answer. How to get Bitmap from an Uri?

Hope this helps.

Community
  • 1
  • 1
Ramesh
  • 1,287
  • 1
  • 9
  • 14