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"/>
时间: 2024-10-18 21:38:09