用开源的Xutil的BitMapUtils的话 是可以显示网络下载的第一张图片的。
然后我自己写了个bitmapUtils,写完以后,其他图片显示没有问题,就是不会显示第一张图片。
不知道问题出在哪里。
用的是recycleview展示图片。
以下是代码:
1.这个方法就是设置图片的方法,除了第一张图片,其他图片加载都没有问题。
utils.display(mHolder.mImage,mData.get(position).listimage);
2.在MyBitmapUtils的类里面的方法
public void display(ImageView mImage, String url) {
//网络加载图片
mNetCaCheUtils.getBitMapFromNet(mImage,url);
}
3.以下是NetCaCheUtils整个类的代码。
public class NetCaCheUtils {
private ImageView imageView;
private String url;
public void getBitMapFromNet(ImageView mImage, String url) {
new BitMapTask().execute(mImage, url);
}
class BitMapTask extends AsyncTask<Object, Integer, Bitmap> {
@Override
protected void onPostExecute(Bitmap o) {
if (o != null) {
//给imageview设置图片
imageView.setImageBitmap(o);
}
}
//更新下载时时
@Override
protected void onProgressUpdate(Integer[] values) {
super.onProgressUpdate(values);
}
@Override
protected Bitmap doInBackground(Object[] params) {
imageView = (ImageView) params[0];
url = (String) params[1];
//下载图片
Bitmap bitmap=null;
try {
bitmap = download(url);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
public Bitmap download(String url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = conn.getInputStream();
//根据输入流生成一个bitmap
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
if (conn != null) {
conn.disconnect();
}
return null;
}
}