2015-06-25 78 views
1

這是我的以下AsyncTask下載圖像的類代碼RecyclerView在Android中使用asynctask下載圖像回收站查看

public class MyDownloadImageAsyncTask extends AsyncTask<String, Void,Bitmap> { 
    private final WeakReference<ImageView> imageViewReference; 

    public MyDownloadImageAsyncTask(ImageView imv) { 
     imageViewReference = new WeakReference<ImageView>(imv); 
    } 

    @Override 
    protected Bitmap doInBackground(String... urls) { 
     Bitmap bitmap = null; 
     for (String url : urls) { 
      bitmap = MyUtility.downloadImage(url); 
      /*if (bitmap != null) { 
       mImgMemoryCache.put(url, bitmap); 
      }*/ 
     } 
     return bitmap; 
    } 

    protected void onPostExecute(Bitmap bitmap){ 
     if (imageViewReference != null && bitmap != null) { 
      final ImageView imageView = imageViewReference.get(); 
      if (imageView != null) { 
       imageView.setImageBitmap(bitmap); 
       } 
      } 
     } 
    } 

我叫AsyncTaskAdapter這樣:

MyDownloadImageAsyncTask task = new MyDownloadImageAsyncTask(holder.vIcon); 
task.execute(new String[] {(String)movie.get("image")});); 

該應用程序崩潰每次運行它的時候。下載圖片的網址爲ArrayList

我想我在調用AsyncTask時這樣做的錯誤,但我找不出解決方案。

+1

考慮使用https://github.com/nostra13/Android-Universal-Image-Loader將圖像下載到您的imageView中....這麼簡單 –

+2

我使用http://square.github.io/picasso/解決Android中的圖像細微差異 –

+1

如果您希望人們幫助診斷您的問題,則需要發佈崩潰。或者像Yogesh建議的那樣看看畢加索。 –

回答

3

更改此

public MyDownloadImageAsyncTask(ImageView imv) { 
     imageViewReference = new WeakReference(imv); 
    } 

這個

public MyDownloadImageAsyncTask(ImageView imv) { 
    imageViewReference = new WeakReference<ImageView>(imv); 
} 

這裏是我使用的代碼和它的作品完美

class LoadImage extends AsyncTask<String, Void, Bitmap> { 

private final WeakReference<ImageView> imageViewReference; 

public LoadImage(ImageView imageView) { 
    imageViewReference = new WeakReference<ImageView>(imageView); 
} 

@Override 
protected Bitmap doInBackground(String... params) { 
    try { 
     return downloadBitmap(params[0]); 
    } catch (Exception e) { 
     // log error 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(Bitmap bitmap) { 
    if (isCancelled()) { 
     bitmap = null; 
    } 

    if (imageViewReference != null) { 
     ImageView imageView = imageViewReference.get(); 
     if (imageView != null) { 
      if (bitmap != null) { 
       imageView.setImageBitmap(bitmap); 
      } else { 
       Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.ic_launcher); 
       imageView.setImageDrawable(placeholder); 
      } 
     } 
    } 
} 

private Bitmap downloadBitmap(String url) { 
    HttpURLConnection urlConnection = null; 
    try { 
     URL uri = new URL(url); 
     urlConnection = (HttpURLConnection) uri.openConnection(); 
     int statusCode = urlConnection.getResponseCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      return null; 
     } 

     InputStream inputStream = urlConnection.getInputStream(); 
     if (inputStream != null) { 
      Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
      return bitmap; 
     } 
    } catch (Exception e) { 
     urlConnection.disconnect(); 
     Log.w("ImageDownloader", "Error downloading image from " + url); 
    } finally { 
     if (urlConnection != null) { 
      urlConnection.disconnect(); 
     } 
    } 
    return null; 
} 

}

這裏是如何我從我的調用它

new LoadImage(holder.itemImage).execute(IMAGE_URL); 

分別爲每個URL。

試試這個,如果它可以幫助你。

+1

這不會導致問題。類型擦除意味着你的兩個代碼段在運行時意味着同樣的事情。 –

+0

我已經改變了你所說的代碼,但沒有進展 –

+0

非常感謝Neal。你是一個救世主_/\ _ –

相關問題