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,318
  • 3
  • 25
  • 40
  • 1
    Add large heap in Manifest file android:largeHeap="true" – Anuja Aug 01 '14 at 09:38
  • @adcom : can you tell me what are the benefits of that?? – Pragnesh Ghoda シ Aug 01 '14 at 09:39
  • check this link , http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object – Rajesh Mikkilineni Aug 01 '14 at 09:39
  • [this could be of some help](http://stackoverflow.com/questions/23165847/how-do-i-solve-this-java-outofmemoryerror/23165878#23165878) – Pararth Aug 01 '14 at 09:40
  • Do not add largeHeap in your app – José Barbosa Aug 01 '14 at 09:41
  • Android has some great info about this to: http://developer.android.com/training/displaying-bitmaps/index.html The fastest way is to use a lib like picasso or universal image loader. I prefer the latter because all settings can be customized like disk cache, animations, .. – Jordy Aug 01 '14 at 09:45
  • @JoséBarbosa what are the Causes of adding LargeHeap?? – Pragnesh Ghoda シ Aug 01 '14 at 09:45
  • lazy loding lib like universal lazy loading will cache ur url hence wont get OOM execpection – KOTIOS Aug 01 '14 at 09:45
  • By the way... you should never `System.gc()`. It will never avoid an OOM exception and it only ever slows down your app. – Philip Couling Aug 01 '14 at 09:49
  • 1
    largeHeap=true does not guarantee that you have enough memory to load your images. And if it works, the app will not crash with 30 images, but will crash with 50 or 60 images. If your ImageView have 50px*50px you should load to memory a resized image with 50*50, and picasso does that for you. – José Barbosa Aug 01 '14 at 09:52
  • @couling : yeah buddy.. i know that issue..thats why i removed it..... – Pragnesh Ghoda シ Aug 01 '14 at 09:53

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
  • 25
  • 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.