做了一个读取网络图片的demo,却总是显示有连接错误,大家帮我看下吧
我是在jdk1.7 eclipse3.7 和avd Google2.2下面做的,Tomcat版本是7.0。
在eclipse中配置了tomcat插件后,我在ie中输入我的图片地址(http://127.0.0.1/tomcat.png)是可以打开这个tomcat小图片的,说明tomcat配置没有问题。
在模拟器中调试之后,显示“读取数据错误”,应该就是下面
if(e instanceof IOException){
Toast.makeText(this, "读取数据错误 ", Toast.LENGTH_SHORT).show();
}
打印出来的错误。
大家帮我看看,哪里连接有问题?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/please_input_address" /> <EditText android:id="@+id/et_address" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://127.0.0.1/tomcat.png" android:lines="2" /> <Button android:id="@+id/bt_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/bt_view_text" /> <ImageView android:id="@+id/iv_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" /></LinearLayout>
package cn.itcast.viewer;import java.io.IOException;import java.net.SocketTimeoutException;import cn.itcast.viewer.service.ImageUtil;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 DemoActivity 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.et_address); mBtView = (Button) this.findViewById(R.id.bt_view); mIvView = (ImageView) this.findViewById(R.id.iv_image); mBtView.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.bt_view: //按钮对应的点击事件 String address = mEtAddress.getText().toString().trim(); if("".equals(address)){ Toast.makeText(this, "图片地址不能为空", Toast.LENGTH_SHORT).show(); return; } try { Bitmap bitmap = ImageUtil.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; } }}
package cn.itcast.viewer.service;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import cn.itcast.viewer.util.StreamTool;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class ImageUtil {/** * 获取网络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; }}
package cn.itcast.viewer.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; }}