2013-04-11 1 views
0

내가이 사람 Listview shows wrong imageslistview에서 이미지 뷰를 캐시하는 방법은 무엇입니까?

과 같은 문제가 있습니다 나는 그것을 캐시해야하는 솔루션을 보았다. 그러나 내 캐시에서 솔루션에 제공된 캐시를 적용하는 방법을 잘 모르겠습니다. 계속 오류가 발생합니다.

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 

    } 

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.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); 
      //result.recycle(); 
     } 
    } 

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

} 

편집

나는 DownloadImageTask.java이 코드에 따라 일부 캐싱을 할 수정 한 : https://stackoverflow.com/a/7730547

하지만이 줄에 문제가있는 것 같습니다 :

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 ... 행 때문에 앱이 항상 중단됩니다. 이것은 초조해지고 있습니다. 도움이 필요해. 도와주세요. 아래는 DownloadImageTask.Java

대신 수동으로 AsyncTask을 사용하고 같은 당신이 캐시 라이브러리를 사용할 수있는이 구현
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; 
     } 
    } 






} 
+0

안녕 모두를 ... 내가 한 응답을받을 수 있지만, 난 내가 뭐하는 거지 라인을 따라 뭔가가 필요합니다 : 당신이 여기 확인할 수 있습니다 이미지 로딩이 라이브러리를 사용하는 방법에 대한 자세한 내용은

. – OneNation

답변

0

을 수정됩니다. 안드로이드 쿼리는 하나 꽤 좋은 : http://code.google.com/p/android-query/

당신이 당신의 ListView 어댑터의 getView 방법 안에 다음과 같은 코드를 사용할 수 있습니다 안드로이드 쿼리 이미지를로드하려면 :

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

이것을 holder.icon.setImageBitmap(bitmapList[position]); 전화를 교체해야합니다.

이렇게하면 이미지 뷰에서 Uri의 이미지를로드하고 캐시하고 표시합니다. 이미지가 이미 캐시 된 경우 대신 캐시에서 자동로드됩니다. http://code.google.com/p/android-query/wiki/ImageLoading

+0

아직도 내 문제가 해결되지 않습니다. mContext에 오류가 있습니다. 나는 'this', 'Adapter.this', 'Adapter.java'로 바꾸었고 여전히 문제가있다 ... – OneNation

+0

나는 대답을 편집했다. mContext 대신 mActivity를 사용하십시오. –

+0

작동하지 않습니다. 나는 모르겠다 ... @ __ @ 그런데, DownloadImageTask.java에서 내 게시물을 업데이트했다. 나는 그것을 작동시킬 수 없다. – OneNation

관련 문제