1

I am developing an application which downloads Images from Server API...
We have created an API which gives JSON response of the image URL...

I have created GridView for displaying images and it is working successfully....

But the problem is that when the number of images increases it throws an OutOfMemoryException because of the increased heap memory...

I tried to clear heap memory manually by System.gc();.. but with no effect.......

So, my question is: How to load all images successfully from the server without having this issue.. What Changes should I make to solve this problem

There is my JSON and Code..

JSON :

{
is_error: "false",
is_error_msg: "Video/Image List",
images: [
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/Screenshot_2014-07-21-17-22-43.png",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/IMG_20140521_155703.jpg",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/IMG_20140522_152254.jpg",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/1386231199182.jpg",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/DSC01809.JPG"
],
videos: [ ],
others: [ ]
}

ImageAdapter.java

private class ImageAdapter extends BaseAdapter {

    private final Context mContext;
    ArrayList<String> urls_list;
    ProgressBar pbrLoadImage;

    public ImageAdapter(Context context,ArrayList<String> urls_list) {
        super();
        mContext = context;
        this.urls_list= urls_list;
    }

    @Override
    public int getCount() {
        return urls_list.size();
    }

    @Override
    public Object getItem(int position) {
        return urls_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes
            LayoutInflater mInflater = (LayoutInflater)
                    getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.image_grid_fragment_raw, null);
        }
        FrameLayout frm = (FrameLayout)convertView.findViewById(R.id.imageframe);
        pbrLoadImage =(ProgressBar)convertView.findViewById(R.id.pbrLoadImage);
        imageView = (ImageView)convertView.findViewById(R.id.imageFromUrl); ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.getLayoutParams().height = 160;
        imageView.getLayoutParams().width = 160;

        Log.i("values", "value :"+url_list.get(position));

        new BitmapWorkerTask(imageView).execute(url_list.get(position)); // calling class for download Images...
        return convertView;
    }

    //download Images from path
    class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        //private int data = 0;
        String path=null;
        Bitmap mIcon=null;

        public BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        // Decode image in background.
        @Override
        protected Bitmap doInBackground(String... params) {
            //data = params[0];
            path = params[0];

            InputStream in=null;
            ByteArrayOutputStream out=null;
            try {
                in = new java.net.URL(path).openStream();
                mIcon = BitmapFactory.decodeStream(in);
                out= new ByteArrayOutputStream();
                mIcon.compress(Bitmap.CompressFormat.PNG, 100, out);
                in.close();
                out.close();

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


            //return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
            //return BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
            return mIcon;
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {

            if (imageViewReference != null && bitmap != null) {

                final ImageView imageView = imageViewReference.get();

                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }
}

Please tell me what changes I should make to solve my problem..

Thanks for your help in advance...

Pragnesh Ghoda シ
  • 8,248
  • 3
  • 24
  • 40

3 Answers3

3

That's a pretty common problem in android. Loading bitmaps into memory may cause OutOfMemoryException.

I recommend you to use a lib like picasso, this lib loads images for you in background and resize them to fit your ImageView with one line of code.

Picasso.with(context).load(URL).fit().centerCrop().into(imageView);
José Barbosa
  • 913
  • 8
  • 17
0

Add BitmapFactory.Options inPurgeable also Calculate the maximum possible inSampleSize For ref Check this and this link.

Prachi
  • 2,559
  • 4
  • 24
  • 37
0
  1. I have checked the images on the browser which you are fetching from the server and found that images are of very high resolution around 960*1280 and you are using the image in a GridView which is of 160*160 pixels and the bitmap so downloaded is very large as compared to the size you requires, so if possible, since you are managing the API as you told, you can upload the images to the server of optimum size not very large , because that is just wasting network bandwidth since more data has to be fetched and more time in fetching and more memory to hold the image in memory.

  2. Use minimum API level to 11 since bitmaps are stored in native memory prior API level 11 which can not be garbage collected by garbage collector on the contrary starting from API level 11 bitmaps are stored in dalvik(Android virtual machine like Java Virtual Machine) heap memory which can be garbage collected when the garbage collector runs.

  3. call bitmap.recycle(); after using the created bitmap, and use these two line snippets. System.gc(); Runtime.getRuntime().gc();

  4. Be extra cautious while using any stream or database related operations , since if streams or database are not properly closed they causes elusive memory leak.

  5. Also I dont know why you are saving the PNG compressed image to the internal memory using output stream : out= new ByteArrayOutputStream(); mIcon.compress(Bitmap.CompressFormat.PNG, 100, out);

  6. using android:largeHeap="true" is not the right approach to solve memory issues , it just delays the crash never completely prevents out of memory crash.