Android 通过URI加载网络图片bitmap

请求网络要在子线程中进行,对UI的操作要放在主线程

package com.example.picturefromnet; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends Activity implements View.OnClickListener { private ImageView iv_icon; private EditText et_url; private Button btn_submit; private final int SUCCESS = 0; private final int FAIL = 1; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SUCCESS: Bitmap bitmap = (Bitmap) msg.obj; iv_icon.setImageBitmap(bitmap); //设置imageView显示的图片 break; case FAIL: Toast.makeText(MainActivity.this, "图片加载失败", Toast.LENGTH_LONG).show(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_icon = (ImageView) findViewById(R.id.iv_icon); et_url = (EditText) findViewById(R.id.et_url); btn_submit = (Button) findViewById(R.id.btn_submit); btn_submit.setOnClickListener(this); } @Override public void onClick(View v) { final String url = et_url.getText().toString().trim(); new Thread(new Runnable() { @Override public void run() { Bitmap bitmap = getImageFromNet(url); if (bitmap != null) { Message msg = new Message(); msg.what = SUCCESS; msg.obj = bitmap; handler.sendMessage(msg); } else { Message msg = new Message(); msg.what = FAIL; handler.sendMessage(msg); } } }).start(); } private Bitmap getImageFromNet(String url) { HttpURLConnection conn = null; try { URL mURL = new URL(url); conn = (HttpURLConnection) mURL.openConnection(); conn.setRequestMethod("GET"); //设置请求方法 conn.setConnectTimeout(10000); //设置连接服务器超时时间 conn.setReadTimeout(5000); //设置读取数据超时时间 conn.connect(); //开始连接 int responseCode = conn.getResponseCode(); //得到服务器的响应码 if (responseCode == 200) { //访问成功 InputStream is = conn.getInputStream(); //获得服务器返回的流数据 Bitmap bitmap = BitmapFactory.decodeStream(is); //根据流数据 创建一个bitmap对象 return bitmap; } else { //访问失败 Log.d("lyf--", "访问失败===responseCode:" + responseCode); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); //断开连接 } } return null; } }


布局文件

<?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"
    android:background="#fff">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_url"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="http://mingxing.facang.com/uploads/allimg/150528/1022435210-1.jpg"
            android:singleLine="true"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:text="GO"
            android:gravity="center"
            android:textSize="20sp"/>

    </LinearLayout>
</LinearLayout>


最后别忘了添加权限

<uses-permission android:name="android.permission.INTERNET"> </uses-permission>

你可能感兴趣的:(Android 通过URI加载网络图片bitmap)