查看图片
效果图:
界面中有三个控件,一个EditText,一个Button,一个ImageView。
1.activity_main布局代码:
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 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <ImageView 13 android:id="@+id/ivImage" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" 16 android:layout_weight="1" 17 android:background="@drawable/back_button" /> 18 19 <Button 20 android:id="@+id/btnView" 21 android:layout_width="143dp" 22 android:layout_height="wrap_content" 23 android:onClick="showImage" 24 android:paddingLeft="20px" 25 android:layout_marginLeft="90px" 26 android:text="浏览" /> 27 28 <EditText 29 android:id="@+id/etImageUrl" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:ems="10" 33 android:hint="http://pica.nipic.com/2007-11-09/2007119124513598_2.jpg" 34 > 35 36 <requestFocus /> 37 </EditText> 38 39 </LinearLayout>
2.MainActivity代码:
1 package com.example.chakan; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Looper; 6 import android.os.Message; 7 8 import android.app.Activity; 9 import android.graphics.Bitmap; 10 import android.graphics.BitmapFactory; 11 import android.text.TextUtils; 12 import android.util.Log; 13 import android.view.Menu; 14 import android.view.View; 15 import android.widget.Button; 16 import android.widget.EditText; 17 import android.widget.ImageView; 18 import android.widget.Toast; 19 public class MainActivity extends Activity { 20 private Button btn; 21 private EditText path; 22 private ImageView imgview; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 btn = (Button) findViewById(R.id.btnView); 29 path = (EditText) findViewById(R.id.etImageUrl); 30 imgview = (ImageView) findViewById(R.id.ivImage); 31 32 btn.setKeyListener(new OnClickListener() { 33 @Override 34 public void onClick(View v) { 35 Log.i("CLICK", ((Button) v).getText().toString()); 36 new Thread(runa).start(); 37 } 38 }); 39 } 40 41 public void setView() { 42 String picturepath = path.getText().toString(); 43 byte[] data = null; 44 try { 45 data = ImageService.getImage(picturepath); 46 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// BitmapFactory:图片工厂! 47 Looper.prepare();// 必须调用此方法,要不然会报错 48 Message msg = new Message(); 49 msg.what = 0; 50 msg.obj = bitmap; 51 handler.sendMessage(msg); 52 } catch (Exception e) { 53 Toast.makeText(getApplicationContext(), "获取图片错误", 1).show(); 54 } 55 } 56 57 private Handler handler = new Handler() { 58 @Override 59 public void handleMessage(Message msg) { 60 if (msg.what == 0) { 61 updateImageView((Bitmap) msg.obj); 62 } 63 } 64 65 }; 66 67 private Runnable runa = new Runnable() { 68 @Override 69 public void run() { 70 setView(); 71 } 72 }; 73 74 private void updateImageView(Bitmap bm) { 75 imgview.setImageBitmap(bm); 76 } 77 }
3.添加一个ImageService图片服务类,里面包含一个获取网络数据的方法;
1 package com.example.chakan; 2 3 import java.io.InputStream; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 7 public class ImageService { 8 9 // 获取网络图片的数据 10 public static byte[] getImage(String picturepath) throws Exception { 11 URL url = new URL(picturepath); 12 HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基于http协议的连接对象 13 conn.setConnectTimeout(10);// 10秒; 14 conn.setRequestMethod("GET");// 大写 15 if (conn.getResponseCode() == 200) { 16 InputStream ins = conn.getInputStream(); 17 return StreamTool.read(ins); 18 } 19 return null; 20 } 21 }
4.添加一个流处理工作类StreamTool:
1 package com.example.chakan; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.InputStream; 5 6 public class StreamTool { 7 8 public static byte[] read(InputStream ins) throws Exception { 9 ByteArrayOutputStream outstream = new ByteArrayOutputStream(); 10 byte[] buffer = new byte[1024]; 11 int length = 0; 12 while ((length = ins.read(buffer)) > -1) { 13 outstream.write(buffer, 0, length); 14 } 15 outstream.close(); 16 return outstream.toByteArray(); 17 } 18 }
到此,程序已经完全编写完毕。一定要注意在androidManifest中必须有配置文件,注意线程的使用。