StreamTool:将InputStream转化为ByteArray
package util; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { /** * 把一个inputstream里面的内容转化成一个byte[] */ public static byte[] getBytes(InputStream is) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } is.close(); bos.flush(); byte[] result = bos.toByteArray(); System.out.println(new String(result)); return result; } }NetUtil.java:
package service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import util.StreamTool; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class NetUtil { /** * 获取网络address地址对应的图片 * * @param address * @return bitmap的类型 */ public static Bitmap getImage(String address) throws Exception { // 通过代码 模拟器浏览器访问图片的流程 URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); // 获取服务器返回回来的流 InputStream is = conn.getInputStream(); byte[] imagebytes = StreamTool.getBytes(is); Bitmap bitmap = BitmapFactory.decodeByteArray(imagebytes, 0, imagebytes.length); return bitmap; } }MainActivity
package com.example.entimageviewer; import java.io.IOException; import java.net.SocketTimeoutException; import service.NetUtil; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; 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 EditText mEtAddress; private Button mBtView; private ImageView mIvView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mEtAddress = (EditText) this.findViewById(R.id.editText1); mBtView = (Button) this.findViewById(R.id.chakan); mIvView = (ImageView) this.findViewById(R.id.imageView1); mBtView.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.chakan: // 按钮对应的点击事件 String address = mEtAddress.getText().toString().trim(); if ("".equals(address)) { Toast.makeText(this, "图片地址不能为空", Toast.LENGTH_SHORT).show(); return; } try { Bitmap bitmap = NetUtil.getImage(address); mIvView.setImageBitmap(bitmap); } catch (Exception e) { if (e instanceof SocketTimeoutException) { Toast.makeText(this, "网络连接超时", Toast.LENGTH_SHORT).show(); } else if (e instanceof IOException) { Toast.makeText(this, "读取数据错误 ", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "未知错误 ", Toast.LENGTH_SHORT).show(); } e.printStackTrace(); } break; } } }main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/input_address" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:lines="2" android:text="http://172.16.40.157:8080/tomcat.png" android:layout_height="wrap_content" /> <Button android:id="@+id/chakan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/go" /> <ImageView android:id="@+id/imageView1" android:scaleType="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>