Android获取网页上的图片的代码
16lz
2021-01-23
public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = null; URL url; url = new URL(imgUrl); if (url != null) { // 打开连接 HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(3000);// 设置网络连接超时的时间为3秒 httpURLConnection.setDoInput(true); // 打开输入流 int responseCode = httpURLConnection.getResponseCode(); // 获取服务器响应值 if (responseCode == HttpURLConnection.HTTP_OK) { // 正常连接 inputStream = httpURLConnection.getInputStream(); // 获取输入流 } bitmap = BitmapFactory.decodeStream(inputStream); } } catch (Exception e) { e.printStackTrace(); } return bitmap; }
最好得到了Bitmap就可随便使用了。
需要注意的是,但凡要联网的:1、都要设置访问权;2、放到线程中去处理
<uses-permission android:name="android.permission.INTERNET"/>
更多相关文章
- Android开发学习——android与服务器端数据交互
- android listview 与 一般多线程配合产生的错误,资料汇总
- android 用post方式上传图片到服务器
- Android/iso客户端与服务器安全通信(Oauth2.0+3DES+RSA)
- android 与 服务器通信
- 文件多线程下载实现
- android使用多线程处理
- Android中通过其他线程更新主线程UI
- Android 之retrofit2 之 @body上传服务器是空值