<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_weight="100" android:id="@+id/img_id" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <EditText android:id="@+id/et" android:text="http://cpro.baidu.com/img/cpro_media_small.png" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="瀏覽" android:onClick="getPic" /></LinearLayout>
主文件 java
package com.look.pic; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import org.apache.http.HttpClientConnection; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity { protected static final int CHANG_UI = 1; protected static final int ERROR = 0; private ImageView iv; private Button bt; private EditText et; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if (msg.what == CHANG_UI) { Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); } else if(msg.what == ERROR) { Toast.makeText(MainActivity.this, "獲取圖片失敗", Toast.LENGTH_SHORT).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.img_id); et = (EditText) findViewById(R.id.et); } public void getPic(View view) { final String path = et.getText().toString().trim(); if (TextUtils.isEmpty(path)) { Toast.makeText(this, "url不能爲空", Toast.LENGTH_SHORT); } else { new Thread(){ public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36"); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); Bitmap bit = BitmapFactory.decodeStream(is); Message msg = new Message(); msg.what = CHANG_UI; msg.obj = bit; handler.sendMessage(msg); } else { Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } }.start(); } } }
清單文件增長權限 android
<uses-permission android:name="android.permission.INTERNET"/> apache