你的位置:首页 > 操作系统

[操作系统]Android 网络图片查看器


今天来实现一下android下的一款简单的网络图片查看器

界面如下:

代码如下:

<LinearLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context=".MainActivity" >  <ImageView    android:id="@+id/iv"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_weight="1000" />  <EditText    android:singleLine="true"    android:text="http://img01.sogoucdn.com/app/a/100540002/1047586.jpg"    android:id="@+id/et_path"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="请输入图片路径" />  <Button    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="click"    android:text="浏览" /></LinearLayout>

代码如下:

package com.wuyudong.imagesviewer;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.apache.http.HttpConnection;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {  private EditText et_path;  private ImageView iv;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    et_path = (EditText) findViewById(R.id.et_path);    iv = (ImageView) findViewById(R.id.iv);  }  public void click(View view) {    String path = et_path.getText().toString().trim();    if (TextUtils.isEmpty(path)) {      Toast.makeText(this, "图片路径不能为空", 0).show();    } else {      // 连接服务器get请求获取图片      try {        URL url = new URL(path);        // 根据url发送http的请求        HttpURLConnection conn = (HttpURLConnection) url            .openConnection();        // 设置请求的方式        conn.setRequestMethod("GET");        conn.setConnectTimeout(5000);        conn.setReadTimeout(5000);        conn.setRequestProperty(            "User-Agent",            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");        int code = conn.getResponseCode();        if (code == 200) {          InputStream is = conn.getInputStream();          Bitmap bitmap = BitmapFactory.decodeStream(is);          iv.setImageBitmap(bitmap);        } else {          Toast.makeText(this, "显示图片失败", 0).show();        }      } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();        Toast.makeText(this, "访问获取图片失败", 0).show();      }    }  }}

添加权限:android.permission.INTERNET

运行后报错:android.os.NetworkOnMainThreadException

解释一下,从Honeycomb SDK(3.0)开始,google不再允许网络请求(HTTP、Socket)等相关操作直接在Main Thread类中,其实本来就不应该这样做,直接在UI线程进行网络操作,会阻塞UI、用户体验相当差!

所以,在Honeycomb SDK(3.0)以下的版本,你还可以继续在Main Thread里这样做,在3.0以上,就不行了

具体解决办法见后文