2014-02-13 61 views
0

我必須創建一個畫廊與下載的圖像,我想顯示加載圖像,而我下載它們。 我用這個適配器:在視圖改變爲下載的圖像如何自動更改圖片與從網上下載的圖片?

public class GridViewImageAdapter extends BaseAdapter { 
Context context; 
public ImageView imageView; 
private Activity _activity; 
private ArrayList<String> _filePaths = new ArrayList<String>(); 
private int imageWidth; 

public GridViewImageAdapter(Activity activity, ArrayList<String> filePaths, 
    int imageWidth,Context context1) { 

this._activity = activity; 
this._filePaths = filePaths; 
this.imageWidth = imageWidth; 
this.context=context1; 

}

public int getCount() { 
    return this._filePaths.size(); 
} 

public Object getItem(int position) { 
    return this._filePaths.get(position); 
} 

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

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

    if (convertView == null) { 
     imageView = new ImageView(_activity); 
    } else { 
     imageView = (ImageView) convertView; 
    } 


    imageView.setBackgroundResource(R.drawable.gallery_image); 

    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,imageWidth)); 
    Log.e("Position",position+""); 
new RetreiveBitMap().execute(_filePaths.get(position)); 

return imageView; 
} 

class RetreiveBitMap extends AsyncTask<String,Integer,Bitmap> { 

private Exception exception; 
protected Bitmap doInBackground(String... urls) { 

    Bitmap bm = null; 
    URL myFileUrl =null; 
    BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize = 1; 
    try { 
     myFileUrl= new URL(urls[0]); 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     } 
    try { 

     String encodedfilename = Base64.encodeToString(urls[0].getBytes(),0); 

    //Controllo presenza della cache 
    try { 
    InputStream check = context.openFileInput(encodedfilename); 
    } catch (FileNotFoundException e) { 
    // Se non presente, scarico il file 

    HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
    conn.setDoInput(true); 
    conn.connect(); 
    InputStream is = conn.getInputStream(); 
    //Salvataggio cache 
    FileOutputStream fos = context.openFileOutput(encodedfilename, Context.MODE_PRIVATE); 
    byte[] buffer = new byte[1024]; 
    int len1 = 0; 
    while ((len1 = is.read(buffer)) > 0) { 
    fos.write(buffer,0, len1); 
    } 

    fos.close(); 
    } 

    bm =  BitmapFactory.decodeStream(context.openFileInput(encodedfilename),null,options); 


    } catch (IOException e) { 
Log.e("errore download",e.getMessage()); 
    } 
    return bm; 

} 
protected void onPreExecute() { 

    imageView.setImageResource(R.drawable.spinner); 
} 

protected void onPostExecute(Bitmap imageBit) { 

    imageView.setImageBitmap(imageBit); 
    } 
} 
} 

有了這個代碼,只有最後一張圖像。 出現的圖像應該是網格中的第一個,而不是最後一個!

+0

我剛剛做了使用畢加索(由Square),它需要幾分鐘的時間來實現。至少有兩個可用於Android的免費庫可以做到這一點。 – 323go

回答

0

你在找什麼是一個「懶惰的加載器」的一些變化,將加載內容,因爲它下載。在GridViewListView中完成此操作並不總是那麼明顯,因爲在兩種視圖類型中均存在構建的視圖回收優化。有幾種方法可以解決這個問題。

(1)更新在支持包含圖像url的適配器的Collection中包含的每個Object添加一個字段。最初加載一個默認圖像,當url已知時,更新Object中的字段並調用視圖上的notifyDataSetChanged()以更新。

(2)實現圖片下載的外部圖片緩存。開始顯示與defalt圖像的視圖。當下載完成時,notifyDataSetChanged。檢查getView方法中的新圖像並進行更新。例如 - 使用LruCache(Google如何處理此問題)

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    View v = convertView; 
    if(v == null){ 
     v = activity.getLayoutInflater().inflate(R.layout.your_layout_for_view, null); 
    } 

    ImageView picture = v.findViewById(R.id.picture_view); 
    Bitmap b = memCache.get(KEY_VALUE); 
    if(b != null){ 
     pictureView.setImageBitmap(b); 
    } else { 
     picture.setImageBitmap(memCache.get(DEFAULT_IMAGE_KEY); 
     if(!checkedKeyList.contains(KEY_VALUE){ 
      downloadPictureAndAddToCache(KEY_VALYE); 
      checkedKeyList.add(KEY_VALYE); 
    } 

} 

你會發現上面的例子是半psudeo代碼。那裏有沒有定義的方法。那是因爲我不知道你想下載圖像的對象的結構。然而,總的想法是檢查外部緩存。如果包含該對象的圖像(通過您定義的某個唯一鍵定位),則使用該圖像。如果找不到圖像,請嘗試使用AsyncTask下載該圖像。我添加了一個List<T>,它代表您檢查的一組鍵。這是爲了防止在沒有找到圖片或不存在其中一個關鍵值的情況下發生無限循環。在每種情況下,當圖片完成下載時,請在您的適配器上調用notifyDataSetChanged()以刷新視圖。您可以在AsyncTask之內的onExpressExecute上執行此操作。