您的位置:首页 > 理论基础 > 计算机网络

Android 网络图片加载5种模式

2017-11-22 16:58 309 查看



Android之网络图片加载的5种模式

学了这么久,最近有空把自己用到过的网络加载图片的方式总结了出来,与大家共享,希望对你们有帮助。
此博客包含Android 5种基本的加载网络图片方式,包括普通加载HttpURLConnection、HttpClients、Volley、XUtils、OkHttp等网络加载图片。
其他网络图片加载方式,后续补上。
效果如下图:
               


HttpURLConnection方式:
 

public Bitmap getImageBitmap(String url) {
URL imgUrl = null;
Bitmap bitmap = null;
try {
imgUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imgUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}


HttpClient方式

public Bitmap getImageBitmap(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
HttpResponse resp = httpclient.execute(httpget);
// 判断是否正确执行
if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
// 将返回内容转换为bitmap
HttpEntity entity = resp.getEntity();
InputStream in = entity.getContent();
Bitmap mBitmap = BitmapFactory.decodeStream(in);
// 向handler发送消息,执行显示图片操作
return mBitmap;
}

} catch (Exception e) {
} finally {
httpclient.getConnectionManager().shutdown();
}

return null;
}


 XUtils方式

private void initView() {
// TODO Auto-generated method stub
BitmapUtils bitmapUtils = new BitmapUtils(this);
// 加载网络图片
bitmapUtils.display(imageView,
"http://img.my.csdn.net/uploads/201407/26/1406383290_9329.jpg");

// 加载本地图片(路径以/开头, 绝对路径)
// bitmapUtils.display(imageView, "/sdcard/test.jpg");

// 加载assets中的图片(路径以assets开头)
// bitmapUtils.display(imageView, "assets/img/wallpaper.jpg");

}


OkHttp方式

private void setIamge()
{
String url = "http://img.my.csdn.net/uploads/201407/26/1406383291_8239.jpg";
OkHttpUtils.get().url(url).tag(this)
.build()
.connTimeOut(20000).readTimeOut(20000).writeTimeOut(20000)
.execute(new BitmapCallback() {
@Override
public void onError(Call call, Exception e, int id) {
}

@Override
public void onResponse(Bitmap bitmap, int id) {
imageView.setImageBitmap(bitmap);
}
});
}


 
Volley方式

/***
* ImageRequest加载图片
*/
public void setImg1()
{

ImageRequest request = new ImageRequest(VolleySingleton.imageThumbUrls[0],
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
imageview1.setImageBitmap(bitmap);
}
}, 0, 0, Config.RGB_565,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
imageview1.setImageResource(R.mipmap.ic_launcher);
}
});
VolleySingleton.getVolleySingleton(this.getApplicationContext()).addToRequestQueue(request);
}
/***
* 使用 ImageLoader 加载图片
*/

public void setImg2()
{
com.android.volley.toolbox.ImageLoader mImageLoader;
mImageLoader =   VolleySingleton.getVolleySingleton(this.getApplicationContext()).getImageLoader();
mImageLoader.get(VolleySingleton.imageThumbUrls[1],
//mImageView是ImageView实例
//第2个参数:默认图片
//第2个参数:加载图片错误时的图片
com.android.volley.toolbox.ImageLoader.getImageListener(imageview2,R.mipmap.ic_launcher, R.mipmap.ic_launcher));
}
/**
* 使用NetworkImageView加载图片
*/
public void setImg3()
{
com.android.volley.toolbox.ImageLoader mImageLoader;
mImageLoader = VolleySingleton.getVolleySingleton(this.getApplicationContext()).getImageLoader();
networkImageView.setImageUrl(VolleySingleton.imageThumbUrls[2], mImageLoader);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: