zoukankan      html  css  js  c++  java
  • android 加载网络图片

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:text="加载" />
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>

    网络类

    package com.example.viewwebimagedemo.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class HttpUtils {
    
        private final static String URL_PATH = "http://images.cnblogs.com/cnblogs_com/liuning8023/588559/o_Android.jpg";
    
        public HttpUtils() {
            // TODO Auto-generated constructor stub
        }
    
        public static InputStream getImageViewInputStream() throws IOException {
            InputStream inputStream = null;
    
            URL url = new URL(URL_PATH);
            if (url != null) {
                HttpURLConnection httpURLConnection = (HttpURLConnection) url
                        .openConnection();
                httpURLConnection.setConnectTimeout(3000);
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoInput(true);
                int response_code = httpURLConnection.getResponseCode();
                if (response_code == 200) {
                    inputStream = httpURLConnection.getInputStream();
                }
            }
            return inputStream;
        }
    }
    package com.example.viewwebimagedemo;
    
    import java.io.InputStream;
    
    import com.example.viewwebimagedemo.R;
    import com.example.viewwebimagedemo.utils.HttpUtils;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        private Button button;
        private ImageView imageView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            button = (Button) this.findViewById(R.id.btn);
            imageView = (ImageView) this.findViewById(R.id.imageView);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(access).start();
                }
            });
        }
           // 另一个线程从网络获得图片
        private Runnable access = new Runnable() {
            @Override
            public void run() {
                getImg();
            }
        };
    
        // 获得图片
        public void getImg() {
            try {
                InputStream inputStream = HttpUtils.getImageViewInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                Looper.prepare();// 必须调用此方法,要不然会报错
                Message msg = new Message();
                msg.what = 0;
                msg.obj = bitmap;
                handler.sendMessage(msg);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "获取图片错误", 1).show();
            }
        }
    
    
        
    
    
        // 更新UI信息
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    if (msg.obj == null) {
                        Toast.makeText(getApplicationContext(), "图片不存在", 1).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "加载图片...", 1)
                                .show();
                        setImg((Bitmap) msg.obj);
                    }
                }
            }
        };
           // 加载图片
        private void setImg(Bitmap bm) {
            imageView.setImageBitmap(bm);
        }
    }

    效果:

    记得加入权限:  <uses-permission android:name="android.permission.INTERNET" />

  • 相关阅读:
    JAVA基础-抽象类和接口
    JAVA基础-多态
    JAVA基础-继承机制
    C++(二十七) — 深拷贝、浅拷贝、复制构造函数举例
    C++(二十六) — 构造函数、析构函数、对象数组、复制构造函数
    C++(二十五) — 类的封装、实现、设计
    C++(二十四) — 指向字符的指针为什么可以用字符串来初始化,而不是字符地址?
    C++(二十三) — 内存泄漏及指针悬挂
    C++(二十二) — 指针变量、函数指针、void指针
    C++(二十一) — 引用概念及本质
  • 原文地址:https://www.cnblogs.com/laopo/p/5826482.html
Copyright © 2011-2022 走看看