下载图像时,VM不会让我们分配...字节

时间:2012-06-11 11:13:59

标签: android android-asynctask

我正在构建一个新闻纸应用程序,我需要在gallary视图中显示新闻纸图像(epaper)...我需要下载大约100张图像。为此,我使用asyncTask,并为每个下载图像我创建新的AsyncTask对象,当我尝试下载图像并设置为gallary我在中间有错误“VM将不允许我们分配...字节”并崩溃应用

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();
                    }
                }


            }

在logcat中显示错误: enter image description here

请帮帮我

提前致谢。

2 个答案:

答案 0 :(得分:1)

你绝对不应该同时在内存上保留100个位图。您只需下载所需的位图,然后在下载新位图之前调用recycle()。

看看这个做法的首选方法:ImageDownloader

答案 1 :(得分:0)

我使用的另一个图像加载选项是Prime,我在所有项目中都使用它,非常简单有效。