zoukankan      html  css  js  c++  java
  • Android获取网页上的图片的代码

    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"/>
  • 相关阅读:
    springboot笔记(五章)整合持久层技术
    Java基础复习(一)
    springboot复习
    JUC(2)
    JUC(1)
    springboot笔记(一~四章)入门、基础配置、整合视图技术、整合Web开发
    JVM复习
    NIO(New IO)
    hive 函数
    hive基础知识
  • 原文地址:https://www.cnblogs.com/wytings/p/4106729.html
Copyright © 2011-2022 走看看