标签:
界面:
代码xml:
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="com.zzw.watctImage.MainActivity" > 7 8 <ImageView 9 android:id="@+id/image_view" 10 android:layout_width="match_parent" 11 android:src="@drawable/t01a7b264978bb316f1" 12 android:layout_height="wrap_content" 13 android:layout_weight="1" /> 14 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:orientation="horizontal" > 19 20 <EditText 21 android:id="@+id/url_edit" 22 android:layout_width="wrap_content" 23 android:layout_height="match_parent" 24 android:layout_weight="1" 25 android:singleLine="true" 26 android:text="http://p1.so.qhimg.com/t017cf2863df563308b.jpg" /> 27 28 <Button 29 android:id="@+id/get" 30 android:layout_width="wrap_content" 31 android:layout_height="match_parent" 32 android:text="获得图片" 33 android:textColor="@android:color/holo_blue_light" /> 34 </LinearLayout> 35 36 </LinearLayout>
java代码中通过点击获得图片按钮得到编辑好的URL的图片
java代码:
1 package com.zzw.watctImage; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 import java.net.URLConnection; 9 10 import android.app.Activity; 11 import android.graphics.Bitmap; 12 import android.graphics.BitmapFactory; 13 import android.os.Bundle; 14 import android.os.Handler; 15 import android.os.Message; 16 import android.view.Menu; 17 import android.view.MenuItem; 18 import android.view.View; 19 import android.view.View.OnClickListener; 20 import android.widget.Button; 21 import android.widget.EditText; 22 import android.widget.ImageView; 23 24 public class MainActivity extends Activity implements OnClickListener { 25 ImageView image_view = null; 26 EditText image_uri = null; 27 Handler handler = new Handler() { 28 @Override 29 public void handleMessage(Message msg) { 30 // TODO Auto-generated method stub 31 super.handleMessage(msg); 32 if (msg.what == 0) { 33 Bitmap bitmap = (Bitmap) msg.obj; 34 image_view.setImageBitmap(bitmap); 35 } 36 37 } 38 39 }; 40 /** 41 * 1.访问网络不能直接放在主方法里面(android.os.NetworkOnMainThreadException), 应该放在一个线程里面 42 * 2.android.view.ViewRootImpl$CalledFromWrongThreadException: 43 * 只能在主线程或者UI线程里面修改视图,应该用通信来解决 44 */ 45 @Override 46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.activity_main); 49 init(); 50 } 51 52 // 初始化 53 public void init() { 54 image_view = (ImageView) findViewById(R.id.image_view); 55 image_uri = (EditText) findViewById(R.id.url_edit); 56 findViewById(R.id.get).setOnClickListener(this); 57 } 58 59 @Override 60 public void onClick(View v) { 61 final String uri = image_uri.getText().toString(); 62 new Thread(new Runnable() { 63 @Override 64 public void run() { 65 // TODO Auto-generated method stub 66 Bitmap bitmap = getBitmap(uri); 67 if (bitmap != null) { 68 Message msg = new Message(); 69 msg.what = 0;// 说明你是谁 70 msg.obj = bitmap; 71 handler.sendMessage(msg); 72 } 73 74 } 75 }).start(); 76 ; 77 } 78 79 // bitmap------>位图 80 public Bitmap getBitmap(String uri) { 81 HttpURLConnection conn = null; 82 try { 83 // 1、获得图片的url 84 URL url = new URL(uri); 85 86 // 2、获得网络连接 87 conn = (HttpURLConnection) url.openConnection(); 88 89 // 3、设置请求的一些常用参数 90 conn.setReadTimeout(3000);// 设置连接去读取数据的最长时间 91 conn.setConnectTimeout(3000);// 设置超时 92 conn.setDoInput(true);// 设置请求可以让服务器写入数据 93 94 // 4、真正的请求图片,然后把从服务器上请求的二进制流保存到inputStream里面 95 conn.connect(); 96 InputStream in = conn.getInputStream(); 97 Bitmap bitmap = BitmapFactory.decodeStream(in); 98 return bitmap; 99 } catch (MalformedURLException e) { 100 // TODO Auto-generated catch block 101 e.printStackTrace(); 102 } catch (IOException e) { 103 // TODO Auto-generated catch block 104 e.printStackTrace(); 105 } 106 // 5、关闭网络连接 107 finally { 108 if (conn != null) { 109 conn.disconnect(); 110 } 111 } 112 return null; 113 } 114 }
标签:
原文地址:http://www.cnblogs.com/zzw1994/p/4915246.html