new Thread ( new Runnable ( ) {
@Override
public void run() {
try {
URL url=new URL ( "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2877661996,3369303060&fm=26&gp=0.jpg" );
//创建连接方法
HttpURLConnection connection = (HttpURLConnection) url.openConnection ( );
//连接时间
connection.setConnectTimeout ( 10000 );
//请求方式
connection.setRequestMethod ( "GET" );
//接收语言
connection.setRequestProperty ( "Accept-Language", "zh-CN,zh;q=0.9" );//可能错
//这里是设置压缩
// connection.setRequestProperty ( "Accept-Encoding","gzip,deflate" );
//接收范围:这里是全部
connection.setRequestProperty ( "Accept", "*/*" );
//设置连接
connection.connect ( );
//返回结果码
final int responseCode = connection.getResponseCode ( );
if (responseCode==HttpURLConnection.HTTP_OK){
final InputStream inputStream = connection.getInputStream ( );//得到数据流
final Bitmap bitmap = BitmapFactory.decodeStream ( inputStream );//将流转为bitmap
result_image.setImageBitmap ( bitmap );//建立图片
}else {
Log.d ( TAG,"抱歉,出错了,帅哥");
}
} catch (Exception e) {
e.printStackTrace ( );
}
}
} ).start ();
网络请求的图片展示到ImageView上
最新推荐文章于 2022-12-29 10:23:41 发布