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" />

转载于:https://www.cnblogs.com/laopo/p/5826482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值