码迷,mamicode.com
首页 > 其他好文 > 详细

采用发消息的形式查看网络图片

时间:2016-05-24 00:26:27      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

1、视图

 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     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <ImageView
 9         android:layout_weight="1000"
10         android:id="@+id/iv"
11         android:layout_width="fill_parent"
12         android:layout_height="fill_parent"
13         />
14     <!--  不能用localhost, 否则会用android本事的localhost,我曾经在此错误过-->
15     <EditText
16         android:text="http://img1.mm131.com/pic/1055/1.jpg" 
17         android:id="@+id/et_path"
18         android:layout_width="fill_parent"
19         android:layout_height="wrap_content"
20         android:hint="请输入图片地址"
21         />
22     <Button 
23         android:onClick="lookImage"
24         android:layout_width="fill_parent"
25         android:layout_height="wrap_content"
26         android:text="浏览"
27         />
28 
29 </LinearLayout>

2、权限

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

3、MainActivity代码

 1 package com.example.lookimage;
 2 
 3 import java.io.InputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.MalformedURLException;
 6 import java.net.URL;
 7 
 8 import android.os.Bundle;
 9 import android.os.Handler;
10 import android.os.Message;
11 import android.app.Activity;
12 import android.graphics.Bitmap;
13 import android.graphics.BitmapFactory;
14 import android.text.TextUtils;
15 import android.view.Menu;
16 import android.view.View;
17 import android.widget.EditText;
18 import android.widget.ImageView;
19 import android.widget.Toast;
20 
21 public class MainActivity extends Activity {
22     protected static final int CHANGE_UI=1;
23     private static final int ERROR = 2;
24     private ImageView iv;
25     private EditText et_path;
26     private Handler handler = new Handler(){
27         public void handleMessage(android.os.Message msg){
28             if(msg.what == CHANGE_UI){
29                 Bitmap bitmap = (Bitmap) msg.obj;
30                 iv.setImageBitmap(bitmap);
31             }else if(msg.what == ERROR){
32                 Toast.makeText(MainActivity.this, "显示图片失败", 0).show();
33             }
34         }
35     };
36     
37     @Override
38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         setContentView(R.layout.activity_main);
41         
42         iv = (ImageView)findViewById(R.id.iv);
43         et_path = (EditText)findViewById(R.id.et_path);
44     }
45 
46 
47     public void lookImage(View view){
48         final String path = et_path.getText().toString().trim();
49         if(TextUtils.isEmpty(path)){
50             Toast.makeText(this, "地址不能为空", 0).show();
51         }else{
52             new Thread(){
53                 public void run(){
54                     try {
55                         URL url = new URL(path);
56                         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
57                         conn.setRequestMethod("GET");
58                         conn.setConnectTimeout(5000);
59                         conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
60                         int code = conn.getResponseCode();
61                         if(code == 200){
62                             InputStream is = conn.getInputStream();
63                             Bitmap bitmap = BitmapFactory.decodeStream(is);
64                             //iv.setImageBitmap(bitmap);
65                             Message msg = new Message();
66                             msg.what = CHANGE_UI;
67                             msg.obj = bitmap;
68                             handler.sendMessage(msg);
69                         }else{
70                             Message msg = new Message();
71                             msg.what = ERROR;
72                             handler.sendMessage(msg);
73                         }
74                     } catch (Exception e) {
75                         // TODO Auto-generated catch block
76                         e.printStackTrace();
77                         Message msg = new Message();
78                         msg.what = ERROR;
79                         handler.sendMessage(msg);
80                     }
81                 }
82             }.start();
83         }
84     }
85     
86 }

 

采用发消息的形式查看网络图片

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5521948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!