使用LruCache在内存中存储位图

时间:2013-08-21 23:11:41

标签: android memory-management android-lru-cache

我一直在尝试在我的应用中实现LruCache,但我很难连接点并在不同组件之间传递位图。

我想知道如何在我的应用中集成LruCache。我也希望了解实现LruCache的过程,以便更多细节更好

第一个类是AsyncTask Image Loader,第二个类是我的自定义适配器

的AsyncTask

 public class GetImagesBitmap extends AsyncTask<ImageView, Void, Bitmap>{
    InputStream inputStream;
    ImageView imageView = null;
    String path;




    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        imageView.setImageBitmap(result);
    }

    private Bitmap download_Image(String url) {


        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = client.execute(httpGet);
            final int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                Log.w("ImageDownloader", "Error " + statusCode
                        + " while retrieving bitmap from " + url);
                return null;
            }

            final HttpEntity entity = httpResponse.getEntity();
            if(entity != null){
                inputStream = null;
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
                if(inputStream != null){
                    inputStream.close();
                }
                entity.consumeContent();

        } catch (IOException e) {

            httpGet.abort();
             Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
        }finally{
            if(client != null){
                client.close();
            }
        }

        return null;
        }



    }

自定义视图适配器类

 public class CustomListingListAdapter extends ArrayAdapter<HashMap<String, String>> {





    private ArrayList<HashMap<String, String>> data;
    private Context context;
    private LayoutInflater mInflater;
    private int viewId;
    private String[] tag;
    HashMap<String, String> currentData = new HashMap<String, String>();

    //private static final String TAG_CONTENT = "content";
    //private static final String TAG_RATING = "rating";
    private static final String TAG_NAME = "name";
    private static final String TAG_IMAGE_PATH = "image_path";
    private static final String TAG_PRODUCT_ID = "product_id";
    private static final String TAG_RATING = "rating";

    public CustomListingListAdapter(Context c,
            ArrayList<HashMap<String, String>> data,
            int viewId, String[] tag) {
        super( c, viewId, data);

        this.context = c;
        this.data= data;
        this.viewId = viewId ;
    }

    @Override
    public int getCount() {

        return data.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        Holder holder;

        if (convertView == null) {

            // Inflate the view since it does not exist
            if (vi == null) {
                mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vi = mInflater.inflate(R.layout.list_item_layout, null);

            }

            holder = new Holder();

            holder.Name = (TextView) vi.findViewById(R.id.name);
            holder.pid = (TextView) vi.findViewById(R.id.pid);
            holder.imageView = (ImageView) vi.findViewById(R.id.list_image);
            holder.ratingView = (ImageView) vi.findViewById(R.id.num_stars);

            vi.setTag(holder);  
        }else {
            holder = (Holder) vi.getTag();
        }


        currentData = (HashMap<String, String>) data.get(position);
        holder.imageView.setTag(currentData.get(TAG_IMAGE_PATH));





        if (currentData != null) {

            holder.Name.setText(currentData.get(TAG_NAME));
            holder.pid.setText(currentData.get(TAG_PRODUCT_ID));
        }   

        new GetImagesBitmap().execute(holder.imageView);
    return vi;


    }
    private static class Holder {

        public ImageView imageView;
        public ImageView ratingView;
        public TextView pid;
        public TextView Name;

       } 
    }

1 个答案:

答案 0 :(得分:5)

你走在正确的轨道上,但是我无法分辨你的代码在List / GridView中的视图回收。您确实重用了传递给getView的convertView,但是在每个getView中启动一个新的AsyncTask将导致加载图像的任务,但是如果启动它的视图被回收,则将它们插入到错误的列表行中。我的建议是查看谷歌的新网络库Volley。它有一个NetworkImageView类,可以将图像异步加载到ImageView中。我推荐它的原因是缓存,无论是在磁盘上还是在内存中,都会为您处理视图回收。

排球: https://developers.google.com/events/io/sessions/325304728

如果你想自己解决这个问题,我会查看关于在ListView中加载位图的Android Developer博客文章。它演示了如你所愿使用的AsyncTasks,但它正确处理视图回收。

文章: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

最后一点提示:不要为每个网络调用实例化一个新的HttpClient。创建一个需要一些时间,首选方法是将一个配置为多线程的HttpClient保存为Singleton实例,并将其用于网络调用。或者,您可以为每个请求创建一个新的HttpUrlConnection;那个班级没问题。

编辑:抱歉,我刚才意识到我实际上没有回答这个问题。要为Bimaps实现LruCache,您可以通过一些调整从支持库扩展LruCache类。这是我用来缓存Bitamps用于Volley库的类:

// From com.android.volley.toolbox.ImageLoader
public interface ImageCache {
    public Bitmap getBitmap(String url);
    public void putBitmap(String url, Bitmap bitmap);
}

// My ImageCache implementation
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
    private static final int DEFAULT_CACHE_SIZE = (int) (Runtime.getRuntime().maxMemory() / 1024) / 8;

    public BitmapLruCache() {
        this(DEFAULT_CACHE_SIZE);
    }

    public BitmapLruCache(int maxSize) {
        super(maxSize);
    }

    @Override
    public Bitmap getBitmap(String url) {
        return get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value == null ? 0 : value.getRowBytes() * value.getHeight() / 1024;
    }
}