Android 如何在listview中缓存imageview?

Android 如何在listview中缓存imageview?,android,listview,caching,bitmap,imageview,Android,Listview,Caching,Bitmap,Imageview,我和这家伙有同样的问题 我看到了我必须缓存它的解决方案。但是在我的缓存中,我不确定如何应用解决方案中给出的缓存。我一直收到错误 我怀疑这是我遇到的问题: holder.icon.setImageBitmap(bitmapList[position]); //loadImageBitmap(mObjects.get(position), position); //onBitmapLoaded(position, holder.mTask.myB

我和这家伙有同样的问题

我看到了我必须缓存它的解决方案。但是在我的缓存中,我不确定如何应用解决方案中给出的缓存。我一直收到错误

我怀疑这是我遇到的问题:

holder.icon.setImageBitmap(bitmapList[position]);

            //loadImageBitmap(mObjects.get(position), position);

            //onBitmapLoaded(position, holder.mTask.myBitmap());
下面是代码。 Adapter.java 公共类适配器扩展了BaseAdapter{

    private static final String TAG = "Adapter";
    private Activity mActivity;
    public ArrayList<Data> mObjects;

    static class ViewHolder {
        ImageView icon;
        TextView title;
        WebView newimage;
        DownloadImageTask mTask;
        public void setOnClickListener(OnItemClickListener onItemClickListener) {
            // TODO Auto-generated method stub

        }
    }



    private Bitmap[] bitmapList;
    private Bitmap bitmapPlaceholder;  

    private void initBitmapListWithPlaceholders(){ 
    // call this whenever the list size changes
    // you can also use a list or a map or whatever so you 
    // don't need to drop all the previously loaded bitmap whenever 
    // the list contents are modified
        int count = getCount();
        bitmapList = new Bitmap[count];
        for(int i=0;i<count;i++){
             bitmapList[i]=bitmapPlaceholder;
        }
    }

    private void onBitmapLoaded(int position, Bitmap bmp){
    // this is your callback when the load async is done
        bitmapList[position] = bmp;
    }

    public Adapter(Activity activity, ArrayList<Data> mObjects) {

        this.mActivity = (Activity) activity;
        this.mObjects = mObjects;


    }

    public void setObjects(ArrayList<Data> mObjects) {
        this.mObjects = mObjects;
    }

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

        Data item = mObjects.get(position);
        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = mActivity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.item, parent, false);


            ViewHolder viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) rowView.findViewById(R.id.image);
            viewHolder.title = (TextView) rowView.findViewById(R.id.title);

            viewHolder.newimage =(WebView)rowView.findViewById(R.id.newimage);


            rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.title.setText(item.getmTitle());

        //String html = " <html><head></head><body><p font-size='8px'>" +  item.getmImageUrl() + "</p><img src='" + item.getmImageUrl() + "' width='100%' height='100%'></body></html>";
        String html = " <html><head></head><body><img src='" + item.getmImageUrl() + "' width='100%' height='100%'></body></html>";




        holder.newimage.loadData(html, "text/html", "utf-8");

        holder.newimage.setFocusable(false);
        holder.newimage.setClickable(false);
        holder.setOnClickListener(new OnItemClickListener() {

            private Object title;

            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Toast.makeText(Adapter.this,"Button is clicked", Toast.LENGTH_LONG).show();
                Log.e("log_tag",this.title.toString() + " clicked");
            }
        });

        //holder.icon.setBackgroundResource(R.drawable.ic_launcher);
        holder.mTask = new DownloadImageTask(item.getmImageUrl(), holder.icon);


        holder.icon.setImageBitmap(bitmapList[position]);

        //loadImageBitmap(mObjects.get(position), position);

        //onBitmapLoaded(position, holder.mTask.myBitmap());
        if (!holder.mTask.isCancelled()) {
            holder.mTask.execute();
        }


        return rowView;
    }

    @Override
    public int getCount() {

        return (this.mObjects.size());
    }

    @Override
    public Object getItem(int position) {

        return (this.mObjects.get(position));
    }

    @Override
    public long getItemId(int position) {

        return (position);
    }

    public AbsListView.RecyclerListener mRecyclerListener = new RecyclerListener( ){

        public void onMovedToScrapHeap(View view) {
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            DownloadImageTask imagetask = viewHolder.mTask;
            if (imagetask != null) {
                imagetask.cancel(true);
            }
        }

    };

    public void setOnClickListener(OnItemClickListener onItemClickListener) {
        // TODO Auto-generated method stub

    }
由于if…行,应用程序总是崩溃。这让人非常沮丧。我需要帮助。请帮助我。下面是修改后的DownloadImageTask.Java

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;

public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
    public Bitmap g;
    private String mUrl;
    private ImageView mImageView = null;

    public DownloadImageTask(String Url, ImageView imageView) {


        mUrl = Url;
        this.mImageView = imageView;




    }

    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);

        /*
        if (result != null) {
            mImageView.setImageBitmap(result);

        }
        */
        if (!(this.mImageView.getTag().toString()).equals(this.mUrl)) {
            /* The path is not same. This means that this
               image view is handled by some other async task. 
               We don't do anything and return. */
            return;
         }

         if(result != null && mImageView != null){
             mImageView.setVisibility(View.VISIBLE);
             mImageView.setImageBitmap(result);
             mImageView.setTag(this.mUrl);
         }else{
             mImageView.setVisibility(View.GONE);
         }


    }

    protected Bitmap doInBackground(Void... params) {

        Bitmap bitmap = getBitmap(mUrl);
        return bitmap;




    }

    public Bitmap getBitmap(String imageUrl) {


        Bitmap mBitmap = null;
        try {

            /*URL newurl = new URL(imageUrl); 
            mBitmap = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); */
            //mImageView.setImageBitmap(mBitmap);

            /*
            URL url = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream is = conn.getInputStream();
            mBitmap = BitmapFactory.decodeStream(new FlushedInputStream(is));*/


            InputStream in = new java.net.URL(imageUrl).openStream();
            mBitmap = BitmapFactory.decodeStream(new FlushedInputStream (in));


            //this.mImageView.setTag(this.mUrl);
            //mBitmap.recycle();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.g = mBitmap;

            return mBitmap;

    }

    public Bitmap myBitmap() {
        Bitmap fg;
        fg = (Bitmap) g;

        return fg;
    }

    static class FlushedInputStream extends FilterInputStream {
        public FlushedInputStream(InputStream inputStream) {
            super(inputStream);
        }

        @Override
        public long skip(long n) throws IOException {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                    int b = read();
                    if (b < 0) {
                        break;  // we reached EOF
                    } else {
                        bytesSkipped = 1; // we read one byte
                    }
                }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
    }






}
导入java.io.FilterInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.net.MalformedURLException;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.view.view;
导入android.widget.ImageView;
公共类DownloadImageTask扩展了AsyncTask{
公共图书馆;
私人字符串mUrl;
私有图像视图mImageView=null;
公共下载ImageTask(字符串Url,ImageView ImageView){
mUrl=Url;
this.mImageView=imageView;
}
受保护的void onPostExecute(位图结果){
super.onPostExecute(结果);
/*
如果(结果!=null){
设置图像位图(结果);
}
*/
如果(!(this.mImageView.getTag().toString()).equals(this.mUrl)){
/*路径不同。这意味着
图像视图由其他异步任务处理。
我们什么都不做就回来*/
返回;
}
if(结果!=null&&mImageView!=null){
mImageView.setVisibility(View.VISIBLE);
设置图像位图(结果);
setTag(this.mUrl);
}否则{
mImageView.setVisibility(View.GONE);
}
}
受保护位图doInBackground(无效…参数){
位图位图=获取位图(mUrl);
返回位图;
}
公共位图getBitmap(字符串imageUrl){
位图mBitmap=null;
试一试{
/*URL newurl=新URL(imageUrl);
mBitmap=BitmapFactory.decodeStream(newurl.openConnection().getInputStream())*/
//设置图像位图(mBitmap);
/*
URL URL=新URL(imageUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
InputStream is=conn.getInputStream();
mBitmap=BitmapFactory.decodeStream(新的FlushedInputStream(is))*/
InputStream in=newjava.net.URL(imageUrl.openStream();
mBitmap=BitmapFactory.decodeStream(新FlushedInputStream(in));
//this.mImageView.setTag(this.mUrl);
//mBitmap.recycle();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
这个.g=mBitmap;
返回mBitmap;
}
公共位图myBitmap(){
位图fg;
fg=(位图)g;
返回fg;
}
静态类FlushedInputStream扩展FilterInputStream{
公共FlushedInputStream(InputStream InputStream){
超级(输入流);
}
@凌驾
公共长跳过(长n)引发IOException{
长的总重量=0升;
while(totalBytesSkipped
而不是使用
AsyncTask
手动实现此功能,您可以使用缓存库。 Android查询是一个非常好的查询:

要使用Android Query加载图像,可以在ListView适配器的
getView
方法中使用以下代码:

AQuery _aq = new AQuery(mActivity);
_aq.id(R.id.icon).image("IMAGE_URI_TO_LOAD");
这将替换
holder.icon.setImageBitmap(位图列表[position]);
调用

这将从imageview中的Uri加载、缓存和显示图像。如果图像已缓存,则它将从缓存自动加载


有关如何使用此库加载图像的详细信息,请查看此处:

Hi all…虽然我收到了1个响应,但我需要的是我正在做的事情。仍然无法解决我的问题。McContext中有一个错误。我替换为“this”、“Adapter.this”、“Adapter.java”,但仍然存在问题…我已编辑答案是。尝试使用mActivity而不是McContextNot。我不知道…@_;@顺便说一句,我在DownloadImageTask.java上更新了我的帖子。我无法让它工作。无法调用AQuery。新的AQuery(mActivity)。你已经将AQuery库导入到你的项目中了吗?(通过将AQuery jar文件添加到项目的libs文件夹中)
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;

public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
    public Bitmap g;
    private String mUrl;
    private ImageView mImageView = null;

    public DownloadImageTask(String Url, ImageView imageView) {


        mUrl = Url;
        this.mImageView = imageView;




    }

    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);

        /*
        if (result != null) {
            mImageView.setImageBitmap(result);

        }
        */
        if (!(this.mImageView.getTag().toString()).equals(this.mUrl)) {
            /* The path is not same. This means that this
               image view is handled by some other async task. 
               We don't do anything and return. */
            return;
         }

         if(result != null && mImageView != null){
             mImageView.setVisibility(View.VISIBLE);
             mImageView.setImageBitmap(result);
             mImageView.setTag(this.mUrl);
         }else{
             mImageView.setVisibility(View.GONE);
         }


    }

    protected Bitmap doInBackground(Void... params) {

        Bitmap bitmap = getBitmap(mUrl);
        return bitmap;




    }

    public Bitmap getBitmap(String imageUrl) {


        Bitmap mBitmap = null;
        try {

            /*URL newurl = new URL(imageUrl); 
            mBitmap = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); */
            //mImageView.setImageBitmap(mBitmap);

            /*
            URL url = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream is = conn.getInputStream();
            mBitmap = BitmapFactory.decodeStream(new FlushedInputStream(is));*/


            InputStream in = new java.net.URL(imageUrl).openStream();
            mBitmap = BitmapFactory.decodeStream(new FlushedInputStream (in));


            //this.mImageView.setTag(this.mUrl);
            //mBitmap.recycle();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.g = mBitmap;

            return mBitmap;

    }

    public Bitmap myBitmap() {
        Bitmap fg;
        fg = (Bitmap) g;

        return fg;
    }

    static class FlushedInputStream extends FilterInputStream {
        public FlushedInputStream(InputStream inputStream) {
            super(inputStream);
        }

        @Override
        public long skip(long n) throws IOException {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                    int b = read();
                    if (b < 0) {
                        break;  // we reached EOF
                    } else {
                        bytesSkipped = 1; // we read one byte
                    }
                }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
    }






}
AQuery _aq = new AQuery(mActivity);
_aq.id(R.id.icon).image("IMAGE_URI_TO_LOAD");