VM won't let us allocation... bytes while downloading images

I am building a news paper app for that I need to show news paper images(epaper) in gallary view... I need to download about 100 images. for that I use asyncTask and for every download image I create new AsyncTask object and,when I am trying to download image and set into gallary I have the error in middle "VM won't let us allocation... bytes" and crash the app.

new AsyncTask<String, Void, Bitmap> () {

                @Override
                protected Bitmap doInBackground(String... params) {
                    HttpGet httpRequest;
                    try {
                        httpRequest = new HttpGet(new URL(params[0]).toURI());
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
                        HttpEntity entity = response.getEntity();
                        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
                        InputStream is = bufHttpEntity.getContent();
                        return BitmapFactory.decodeStream(is);

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }/* catch (Error e) {
                        e.printStackTrace();
                    }*/
                    return null;
                }

                @Override
                protected void onPostExecute(Bitmap result) {
                    if(result != null) {
                          img.setImageBitmap(ePaperInfo.getImgJpg1());
                        notifyDataSetChanged();
                    }
                }


            }

show the error in logcat:

Please help me

Thanks in advance.

Answers


You should definetly not keep 100 Bitmaps on memory at the same time. You need to download only the Bitmaps needed and then call recycle() before downloading new Bitmaps.

Have a look at this exaple for the preferred way of doing what you want: ImageDownloader


Another option for image loading that I use is Prime, I use it in all of my projects and its pretty simple and efficient.


Need Your Help