在ListView中使用LazyList时出现内存不足错误

时间:2012-12-13 09:30:54

标签: android android-listview lazy-loading android-adapter lazylist

  

可能重复:
  Out Of memory error using Universal Image Loader and images getting refreshed

我一直在关注延迟加载列表视图中的图像的教程,而不是我实现LazyList。代码根据我的要求正常工作,但它在某些阶段给出Out Of Memory错误。我使用相同的ImageLoader,内存缓存,LazyAdapter,FileChache和Utils代码。 这是LazyAdapter类

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<String> movieThumbnail;
    private ArrayList<String> movieText;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<String> movieThumbnail, ArrayList<String> movieText) {
        activity = a;
        /*data=d;*/
        this.movieThumbnail = movieThumbnail;
        this.movieText = movieText;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return movieText.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
              vi = inflater.inflate(R.layout.listrow, null);

        TextView text=(TextView)vi.findViewById(R.id.rowListTextView);
        ImageView image=(ImageView)vi.findViewById(R.id.movieImage);
        text.setText(movieText.get(position));
        imageLoader.DisplayImage(movieThumbnail.get(position), image);
        return vi;
    }
}

的MemoryCache

public class MemoryCache {

    private static final String TAG = "MemoryCache";
    private Map<String, Bitmap> cache=Collections.synchronizedMap(
            new LinkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU ordering
    private long size=0;//current allocated size
    private long limit=1000000;//max memory in bytes

    public MemoryCache(){
        //use 25% of available heap size
        setLimit(Runtime.getRuntime().maxMemory()/6);
    }

    public void setLimit(long new_limit){
        limit=new_limit;
        Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");
    }

    public Bitmap get(String id){
        try{
            if(!cache.containsKey(id))
                return null;
            //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 
            return cache.get(id);
        }catch(NullPointerException ex){
            ex.printStackTrace();
            return null;
        }
    }

    public void put(String id, Bitmap bitmap){
        try{
            if(cache.containsKey(id))
                size-=getSizeInBytes(cache.get(id));
            cache.put(id, bitmap);
            size+=getSizeInBytes(bitmap);
            checkSize();
        }catch(Throwable th){
            th.printStackTrace();
        }
    }

    private void checkSize() {
        Log.i(TAG, "cache size="+size+" length="+cache.size());
        if(size>limit){
            Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated  
            while(iter.hasNext()){
                Entry<String, Bitmap> entry=iter.next();
                size-=getSizeInBytes(entry.getValue());
                iter.remove();
                if(size<=limit)
                    break;
            }
            Log.i(TAG, "Clean cache. New size "+cache.size());
        }
    }

    public void clear() {
        try{
            //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 
            cache.clear();
            size=0;
        }catch(NullPointerException ex){
            ex.printStackTrace();
        }
    }

    long getSizeInBytes(Bitmap bitmap) {
        if(bitmap==null)
            return 0;
        return bitmap.getRowBytes() * bitmap.getHeight();
    }
}

ImageLoade类

public class ImageLoader {

    MemoryCache memoryCache=new MemoryCache();
    FileCache fileCache;
    private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService;
    Handler handler=new Handler();//handler to display images in UI thread

    public ImageLoader(Context context){
        fileCache=new FileCache(context);
        executorService=Executors.newFixedThreadPool(5);
    }

    final int stub_id=R.drawable.ic_launcher;
    public void DisplayImage(String url, ImageView imageView)
    {
        imageViews.put(imageView, url);
        Bitmap bitmap=memoryCache.get(url);
        if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
        else
        {
            queuePhoto(url, imageView);
            imageView.setImageResource(stub_id);
        }
    }

    private void queuePhoto(String url, ImageView imageView)
    {
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private Bitmap getBitmap(String url) 
    {
        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)

            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
           /* bitmap = decodeFile(f);*/
            return bitmap;
        } catch (Throwable ex){
           ex.printStackTrace();
           if(ex instanceof OutOfMemoryError)
               memoryCache.clear();
           return null;
        }
    }

    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1=new FileInputStream(f);
            BitmapFactory.decodeStream(stream1,null,o);
            stream1.close();

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            FileInputStream stream2=new FileInputStream(f);
            Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Task for the queue
    private class PhotoToLoad
    {
        public String url;
        public ImageView imageView;
        public PhotoToLoad(String u, ImageView i){
            url=u; 
            imageView=i;
        }
    }

    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;
        PhotosLoader(PhotoToLoad photoToLoad){
            this.photoToLoad=photoToLoad;
        }

        @Override
        public void run() {
            try{
                if(imageViewReused(photoToLoad))
                    return;
                Bitmap bmp=getBitmap(photoToLoad.url);
                memoryCache.put(photoToLoad.url, bmp);
                if(imageViewReused(photoToLoad))
                    return;
                BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
                handler.post(bd);
            }catch(Throwable th){
                th.printStackTrace();
            }
        }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad){
        String tag=imageViews.get(photoToLoad.imageView);
        if(tag==null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }

    //Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;
        public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
        public void run()
        {
            if(imageViewReused(photoToLoad))
                return;
            if(bitmap!=null)
                photoToLoad.imageView.setImageBitmap(bitmap);
            else
                photoToLoad.imageView.setImageResource(stub_id);
        }

    }

    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

}

1 个答案:

答案 0 :(得分:4)

使用此方法进行图像加载当使用滚动而不是强制关闭时,这不会产生问题

    MainClassActivity:
ArrayList<NewsItems> news = new ArrayList<NewsItems>();
    SetListView sta;
       sta = new SetListView(MainActivity.this, news);
                    lv.setAdapter(sta);

                    for (NewsItems s : news) {
                            s.loadImage(sta);
                    }


        public class SetListView  extends BaseAdapter{
            private LayoutInflater inflater = null;
            Activity activity;
            ArrayList<NewsItems> one;
            public SetListView(Activity a,ArrayList<NewsItems> one)
            {
                this.activity = a;
                inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                this.one = one;
            }

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

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

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View vi = convertView;
                if (convertView == null)
                    vi = inflater.inflate(R.layout.main1, null);
                TextView tv1 = (TextView) vi.findViewById(R.id.textView1);
                TextView tv2 = (TextView) vi.findViewById(R.id.textView2);
                tv1.setText(one.get(position).get_title());
                tv2.setText(one.get(position).get_pudDate());
        //      Bitmap bm = loadBitmap(one.get(position).get_image());
                if(one.get(position).get_bm() != null)
                    ((ImageView)vi.findViewById(R.id.imageView1)).setImageBitmap(one.get(position).get_bm());
                else
                    ((ImageView)vi.findViewById(R.id.imageView1)).setImageResource(R.drawable.ic_launcher);
                return vi;
            }

        }


package com.dudhat.classes;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import com.dudhat.rssfeeds.SetListView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class NewsItems {
    private SetListView sta;
    String _title,_pudDate,_description,_image;
    Bitmap _bm;

    public Bitmap get_bm() {
        return _bm;
    }

    public String get_image() {
        return _image;
    }

    public String get_title() {
        return _title;
    }

    public String get_pudDate() {
        return _pudDate;
    }

    public String get_description() {
        return _description;
    }
    public SetListView getAdapter() {
        return sta;
    }

    public void setAdapter(SetListView sta) {
        this.sta = sta;
    }
    public NewsItems(String _title,String _pubdate,String _description,String _image,Bitmap _bm)
    {
        this._description = _description;
        this._pudDate = _pubdate;
        this._title = _title;
        this._image = _image;
        this._bm = _bm;
    }
     public void loadImage(SetListView sta) {
                 this.sta = sta;
                 if (_image != null && !_image.equals("")) {
                        new ImageLoadTask().execute(_image);
              }
             }
    private class ImageLoadTask extends AsyncTask<String, String, Bitmap> {

        @Override
        protected void onPreExecute() {
            Log.i("ImageLoadTask", "Loading image...");
        }

        protected Bitmap doInBackground(String... param) {
            Log.i("ImageLoadTask", "Attempting to load image URL: " + param[0]);
            try {
                Bitmap b = loadBitmap(param[0]);
                return b;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        protected void onPostExecute(Bitmap ret) {
            if (ret != null) {
                _bm = ret;
                if (sta != null) {
                    sta.notifyDataSetChanged();
                }
            } else {
            }
        }
    }
    public Bitmap loadBitmap(String url)
    {
        Bitmap bm = null;
        InputStream is = null;
        BufferedInputStream bis = null;
        try 
        {
            URLConnection conn = new URL(url).openConnection();
            conn.connect();
            is = conn.getInputStream();
            bis = new BufferedInputStream(is, 8192);
            bm = BitmapFactory.decodeStream(bis);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        finally {
            if (bis != null) 
            {
                try 
                {
                    bis.close();
                }
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
            if (is != null) 
            {
                try 
                {
                    is.close();
                }
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        }
        return bm;
    }
}