Android 网络图片查看器
2014-03-28 17:18
316 查看
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
<?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>
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>
相关文章推荐
- Android实现网络图片查看器和网页源码查看器
- Android实现网络图片查看器和网页源码查看器
- android消息机制之网络图片查看器
- android一个小网络图片查看器
- android 简单网络图片查看器 HttpURLConnection
- 从头学android_网络图片查看器
- Android编程实现网络图片查看器和网页源码查看器实例
- Android小项目01--网络图片查看器
- Android--第四天(网络图片查看器,多线程下载,文件断点上传原理,Intent,Activity生命周期,广播,服务)
- Android smartimageview网络图片查看器
- 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)
- Android——网络编程(网络图片查看器)
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- Android入门之网络图片查看器
- Android项目——网络图片查看器
- Android网络通信之网络图片查看器
- android网络图片查看器简单实现代码
- Android 网络图片查看器
- android 网络图片查看器,Handler的用法
- Android项目——网络图片查看器