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-10-13 15:20:43 发布
本文介绍了一种使用Java从网络加载图片并将其显示在应用程序中的方法。通过创建URL对象和HttpURLConnection,设置请求参数,如连接超时、请求方式等,然后获取输入流并转换为Bitmap对象,最后在界面上展示图片。
1246

被折叠的 条评论
为什么被折叠?



