在android开发中,经常会遇到从后台服务器上获取网络图片,在此我会通过用Tomcat创建一个本地服务器,在该服务器中放置一张图片然后访问它并显示在手机界面上。
1.打开Tomcat服务器,(找到Tomcat安装路径)如图
点击startup.bat会出现下图说明服务器已配置好
然后打开浏览器输入http://localhost:8080会出现如下图所示
接着在服务器中放置一张图片,具体操作:打开tomcat安装路径,在E:\jsp\apache-tomcat-7.0.37\webapps\ROOT文件中放置图片sex.jpg如图所示
接着浏览器输入http://localhost:8080/sex.jpg即可查看图片
至此服务器端就设置好了,现在打开Eclipse,新建一个项目测试访问图片的程序
1.搭建界面文件名为activity_search.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:background="@color/gray"
android:orientation="vertical" >
<!-- <include layout="@layout/activity_search_header" /> -->
<EditText
android:id="@+id/tt_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/imageurl" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
<ImageView
android:id="@+id/net_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.创建一个类继承Activity
/**
*
*/
package com.example.park;
import com.example.park.service.ImageService;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
/**
* 我的资料Activity
*
* @author
* @since
*/
public class MyInfoActivity extends Activity implements OnClickListener
{
private EditText tt_url;
private Button btn_submit;
private ImageView net_image;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
initCompent();
btn_submit.setOnClickListener(this);
}
/**
* 初始化控件
*/
private void initCompent()
{
tt_url = (EditText) this.findViewById(R.id.tt_url);
btn_submit = (Button) this.findViewById(R.id.btn_submit);
net_image = (ImageView) this.findViewById(R.id.net_image);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn_submit:
String path = tt_url.getText().toString();
byte[] data;
try
{
data = ImageService.getImage(path);//一个封装类访问网络数据得到的是byte格式的数据
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//将byte格式的数据解析成Bitmap对象
net_image.setImageBitmap(bitmap);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
}
3.实现ImageService.getImage(path)方法:代码如下
package com.example.park.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.example.park.util.StreamTool;
/**
* 访问网络图片
*
* @author syj
*
*/
public class ImageService
{
public static byte[] getImage(String path) throws Exception
{
URL url = new URL(path);//生成URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//打开链接
connection.setReadTimeout(5000);//设置读取超时时间
connection.setRequestMethod("GET");//设置网络访问方式“GET”
// 判断是否链接成功
if (connection.getResponseCode() == 200)
{
InputStream inStream = connection.getInputStream();//得到输入流
byte[] data = StreamTool.getByteArray(inStream);//一个封装好的类将流文件转换成byte字节数组
return data;
}
return null;
}
}
4.实现StreamTool.getByteArray(inStream)方法:
package com.example.park.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
* 流转换成byte字节数组
*
* @author syj
*
*/
public class StreamTool
{
public static byte[] getByteArray(InputStream inStream) throws Exception
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, length);
}
inStream.close();
return outStream.toByteArray();
}
}
至此编码部分已完成,不过该项目中需要注意点是由于android是个操作系统,windows也是一个操作系统,因此在图片路径的问题上要注意不可在android中奖路径写成http://localhost:8080/sex.jpg这种格式,我们应该通过cmd找出本机IP地址如图
将localhost换成10.223.113.26(这个会经常变化的)此时在浏览器中打开该链接也会得到图片
将该项目部署到模拟器上如图
至此已完成编码测试,如有疑问敬请留言。