位图(列表视图)的内存不足错误



由于位图,我已经阅读了很多关于 android 内存不足的信息,并采取了各种措施来防止它,但它一直在无休止地敲门。这是我的代码:

法典:

    in = httpConn.getInputStream();
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inPurgeable=true; 
   options.inJustDecodeBounds = true;
   options.inSampleSize = 1;
   options.inJustDecodeBounds = false;
   image = BitmapFactory.decodeStream(in, null, options);

我能够加载大约 15 张尺寸为 640*640 的图像。之后,它会在日志猫中向我抛出内存不足错误。我还为"PullToRefresh"功能实现了一个外部库(来自GitHub)。所以很明显,这涉及自定义列表视图。有没有办法避免这个内存问题。

注意:我已经读过这篇文章了。我也实现了这些方法,但它仍然站不住脚。

我可以将片段与列表视图一起使用来避免此内存问题吗?或者,我错过了什么吗?任何帮助将不胜感激。提前谢谢。

只需将options.inSampleSize = 1;更改为options.inSampleSize = 4;

并且还做

bitmap.reCycle();

好吧,现在来自互联网的图像加载时间有很多解决方案。您也可以使用此库"Android-Query"https://code.google.com/p/android-query/wiki/ImageLoading。它将为您提供所有必需的活动。确定你想做什么?并阅读图书馆维基页面。并解决图像加载限制。

这是我的代码:-

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }
     ImageView imageview = (ImageView) v.findViewById(R.id.icon);
           AQuery aq = new AQuery(convertView);
           String imageUrl = "http://www.vikispot.com/z/images/vikispot/android-w.png";
          aq.id(imageview).progress(this).image(imageUrl, true, true, 0, 0, new BitmapAjaxCallback(){
                   @Override
                   public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status){
                       iv.setImageBitmap(bm);

                   }
           });
    }
    return v;
}

最新更新