查看网络图片功能的应用程序
使用Android线程机制来实现这个功能
效果图:
当在EditText中输入地址后,点击浏览按钮,会在EditText上面空白地方显示该地址对应的一张图片。
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_gravity="center" 17 android:layout_weight="1" 18 /> 19 20 <EditText 21 android:id="@+id/etImageUrl" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:ems="10" 25 android:hint="请输入图片的地址" 26 > 27 28 <requestFocus /> 29 </EditText> 30 31 <Button 32 android:id="@+id/btnView" 33 android:layout_width="278dp" 34 android:layout_height="wrap_content" 35 android:background="@drawable/back_button" 36 android:onClick="showImage" 37 android:text="浏览" /> 38 39 </LinearLayout>
MainActivity代码:
1 public class MainActivity extends Activity { 2 Button button; 3 ImageView imageView; 4 EditText ed; 5 private static final int MSG_SUCCESS = 0;// 获取图片成功的标识 6 private static final int MSG_FAILURE = 1;// 获取图片失败的标识 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 findView(); 13 } 14 15 public void findView() { 16 button = (Button) findViewById(R.id.btnView); 17 imageView = (ImageView) findViewById(R.id.ivImage); 18 ed = (EditText) findViewById(R.id.edt); 19 } 20 21 @Override 22 public boolean onCreateOptionsMenu(Menu menu) { 23 // Inflate the menu; this adds items to the action bar if it is present. 24 getMenuInflater().inflate(R.menu.main, menu); 25 return true; 26 } 27 28 private Handler handler = new Handler(){ 29 30 public void handleMessage(Message msg) { 31 switch (msg.what) { 32 case MSG_SUCCESS: 33 Bitmap bit = (Bitmap) msg.obj; 34 imageView.setImageBitmap(bit); 35 break; 36 default: 37 break; 38 } 39 }; 40 41 }; 42 43 public void viewImage(View view) { 44 45 final String path = ed.getText().toString();//获取输入的图片路径 46 if (TextUtils.isEmpty(path)) { 47 Toast.makeText(MainActivity.this, " 路径不为空", Toast.LENGTH_SHORT) 48 .show(); 49 } else { 50 new Thread() { 51 public void run() { 52 URL url; 53 try { 54 url = new URL(path);//发出请求 55 HttpURLConnection connection = (HttpURLConnection) url 56 .openConnection(); 57 connection.setRequestMethod("GET");//设置请求方式这里的方式必须为大写 58 connection.setConnectTimeout(5000);//设置超时的时间 59 int code = connection.getResponseCode();//获得状态码 60 if (code == 200) { 61 InputStream is = connection.getInputStream(); 62 Bitmap bitmap = BitmapFactory.decodeStream(is);//写入一个bitmap流 63 Message m = new Message(); 64 m.what = MSG_SUCCESS; 65 m.obj = bitmap; 66 handler.sendMessage(m); 67 } else { 68 Toast.makeText(MainActivity.this, "打开失败", 69 Toast.LENGTH_SHORT).show(); 70 } 71 72 } catch (MalformedURLException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 75 } catch (IOException e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } 79 80 }; 81 }.start(); //必须加start()方法
82 } 83 84 } 85 }
注意:UI线程只允许修改Activity里的UI组件,然后在androidManifest中必须有配置文件。注意开始线程的使用!!