標籤:android
xml檔案代碼部分
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edit" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:text="http://e.hiphotos.baidu.com/image/pic/item/d52a2834349b033b6764dedb17ce36d3d439bdc6.jpg" /> <Button android:id="@+id/go" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go" android:textSize="20sp" /> </LinearLayout></LinearLayout>
Activity
package com.example.android01;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private ImageView image;private EditText edit;private Button but;private final int success=0;private Handler handler=new Handler(){@Overridepublic void handleMessage(android.os.Message msg) {Bitmap b=(Bitmap)msg.obj;if(b!=null){if(msg.what==success){image.setImageBitmap(b);}}else{Toast.makeText(getApplicationContext(), "擷取圖片錯誤", 0).show();}};}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image=(ImageView) findViewById(R.id.image); edit=(EditText) findViewById(R.id.edit); but=(Button) findViewById(R.id.go); but.setOnClickListener(this); }@Overridepublic void onClick(View v) {final String url=edit.getText().toString();new Thread(new Runnable() {@Overridepublic void run() {Bitmap bit=getImageFromNet(url);//image.setImageBitmap(bit);Message msg=new Message();msg.obj=bit;msg.what=success;handler.sendMessage(msg);}}).start();}private Bitmap getImageFromNet(String url){ HttpURLConnection conn=null;try {URL mURL=new URL(url);//建立一個URL串連 conn=(HttpURLConnection) mURL.openConnection();//得到一個connection對象conn.setRequestMethod("GET");//佈建要求方法為GETconn.setConnectTimeout(10000);//設定連線逾時時間conn.setReadTimeout(5000);//設定讀取過程中的異常conn.connect();int responseCode=conn.getResponseCode();//擷取響應碼,404,500,200if(responseCode==200){//訪問成功InputStream in=conn.getInputStream();Bitmap bitmap=BitmapFactory.decodeStream(in);//將從伺服器擷取的流變成Bitmap位元影像return bitmap;}else{Toast.makeText(this, "擷取圖片失敗", 0).show();}} catch (Exception e) {e.printStackTrace();}finally{if(conn!=null){conn.disconnect();}}return null;} }
本文出自 “Java大白的戰地” 部落格,請務必保留此出處http://8023java.blog.51cto.com/10117207/1665704
Android中子線程網路查看器與Handler訊息處理器