Android 网络图片查看器
由于android下的网络连接不能在主线程操作,另外UI的改变也只能通过主线程来变化,所以一般来说操作网络我们都要开启一个子线程执行,而UI的改变则通过handler消息传递给主线程,委托主线程操作。
以下是一个android网络图片查看器,需要传入一个URL地址来查看该图片:
package com.wuyou.webimgbrowser; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends ActionBarActivity { //常量:改变显示的图片 private static final int CHANGE_IMG = 1; private static final int ERROR = 2; private ImageView imageView; private EditText editText; private Button button; //消息处理器 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == CHANGE_IMG) { Bitmap bitmap = (Bitmap) msg.obj; imageView.setImageBitmap(bitmap); } else if (msg.what == ERROR) { Toast.makeText(MainActivity.this, "获取图片失败!", Toast.LENGTH_LONG).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.iv); editText = (EditText) findViewById(R.id.et); button = (Button) findViewById(R.id.bt); } public void click(View view) { final String path = editText.getText().toString().trim(); //子线程不能修改UI,只能通过消息发送到主线程,让主线程代替修改UI new Thread(new Runnable() { @Override public void run() { if (TextUtils.isEmpty(path)) { sendError(null); } else { try { System.out.println("path = " + path); URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET");//设置以get方式获取资源 httpURLConnection.setConnectTimeout(5000);//设置连接服务器如果没有在5秒钟运行则抛出错误 httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"); if (httpURLConnection.getResponseCode() == 200) { InputStream inputStream = httpURLConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); Message message = new Message(); message.what = CHANGE_IMG; message.obj = bitmap; handler.sendMessage(message); } else { sendError(null); } } catch (Exception e) { sendError(e); } } } //发送错误 private void sendError(Exception e) { if (e != null) { e.printStackTrace(); } Message message = new Message(); message.what = ERROR; handler.sendMessage(message); } }).start(); } }
其中布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" tools:ignore="MergeRootFrame"> <ImageView android:id="@+id/iv" android:layout_weight="3000" android:layout_height="match_parent" android:layout_width="match_parent" /> <EditText android:text="http://image.sjrjy.com/201011/291354164ea84578066309.jpg" android:lines="1" android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt" android:text="浏览图片" android:onClick="click" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </LinearLayout>
最后不要忘了在清单文件添加:(网络连接的权限)
<uses-permission android:name="android.permission.INTERNET" />
本文出自 无忧之路 - 博客园