010_01网络图片查看器

 1 package com.example.day10_01getpicture;
 2 
 3 import java.io.InputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.URL;
 6 
 7 import android.app.Activity;
 8 import android.graphics.Bitmap;
 9 import android.graphics.BitmapFactory;
10 import android.os.Bundle;
11 import android.os.Handler;
12 import android.os.Message;
13 import android.view.View;
14 import android.widget.ImageView;
15 import android.widget.Toast;
16 
17 public class MainActivity extends Activity {
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23     }
24     
25     final static int MSG_DOWNLOAD_OK =1; 
26     final static int MSG_DOWNLOAD_ERROR =2; 
27     
28     /*  Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,
29      *而另外一个部分逐个的在消息队列中将消息取出,然后对消息进行出来,就是发送消息和接收消息不是同步的处理。
30      *这种机制通常用来处理相对耗时比较长的操作*/
31      Handler handler = new Handler(){
32         @Override
33         public void handleMessage(Message msg) {
34             super.handleMessage(msg);
35             //针对message进行处理
36             switch (msg.what) {
37             case MSG_DOWNLOAD_OK:
38                 ImageView iv= (ImageView) findViewById(R.id.iv_pic);
39                 iv.setImageBitmap((Bitmap)msg.obj);
40                 break;
41             case MSG_DOWNLOAD_ERROR:
42                 Toast.makeText(MainActivity.this, "下载失败", 1).show();
43                 break;    
44             default:
45                 break;
46             }         
47         }    
48     };
49 
50     
51     public void getpicture(View v){
52         Thread tread = new Thread(){
53             public void run() {
54                 //启动tomcat服务器
55                 String path = "http://192.168.3.39/tomcat.gif";
56                 try {
57                     URL url = new URL(path);
58                     HttpURLConnection  conn = (HttpURLConnection) url.openConnection();
59                     conn.setReadTimeout(5000);
60                     conn.setRequestMethod("GET");
61                     conn.setConnectTimeout(5000);
62                     // pending;
63                     conn.connect();
64                     
65                     if (conn.getResponseCode()==200) {
66                       InputStream is = conn.getInputStream();
67                       Bitmap bm = new BitmapFactory().decodeStream(is);
68                       
69                      /*子线程内无法操作UI,原因?
70                          ImageView iv= (ImageView) findViewById(R.id.iv_pic);
71                       iv.setImageBitmap(bm); 
72                        让主线程去更新ui--》 通知主线程去做事情,顺便把数据bm给到主线程
73                        */  
74                      // Message  msg = new Message();//ok
75                       Message msg = handler.obtainMessage(); 
76                       msg.what = MSG_DOWNLOAD_OK;
77                       msg.obj  = bm;
78                       handler.sendMessage(msg);                  
79                     }
80                     else {
81                      // Message  msg = new Message();//!ok
82                       Message msg = handler.obtainMessage(); 
83                       msg.what =MSG_DOWNLOAD_ERROR;
84                       handler.sendMessage(msg);
85                         //Toast.makeText(this, "下载失败", 1).show();
86                     }            
87                 }catch (Exception e) {
88                     e.printStackTrace();
89                 }            
90             };
91         };        
92         tread.start();    
93     }
94 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="${relativePackage}.${activityClass}"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="hello_world" />
12     <Button 
13          android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="获取图片" 
16         android:onClick="getpicture"/>
17     <ImageView
18         android:id="@+id/iv_pic"
19          android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21          android:src="@drawable/ic_launcher"
22         ></ImageView>
23 </LinearLayout>

效果图:

物随心转,境由心造,一切烦恼皆由心生。
原文地址:https://www.cnblogs.com/woodrow2015/p/4515053.html