本文实例讲述了Android编程获取图片数据的方法。分享给大家供大家参考,具体如下:

网络的访问在我们日常生活中太重要了,如果没有网络我们的生活将会是什么样子呢?Android手机和浏览器也是一样的,也可以通过网络通讯获取数据,如调用webservice,EJB等。下面就通过一个小例子从网络获取一幅图片并显示在手机上,开发中将会使用到一个新的组件ImageView.

1. 写一个用来处理字节流的工具类

package org.lxh.util;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

public class StreamTool {

public static byte[] readInputStream(InputStream in) throws Exception{

int len=0;

byte buf[]=new byte[1024];

ByteArrayOutputStream out=new ByteArrayOutputStream();

while((len=in.read(buf))!=-1){

out.write(buf,0,len); //把数据写入内存

}

out.close(); //关闭内存输出流

return out.toByteArray(); //把内存输出流转换成byte数组

}

}

2. 写一个得到图片byte数组的service类

package org.lxh.service;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import org.lxh.util.StreamTool;

import android.util.Log;

public class WebService {

public static byte[] getImage(String path){

URL url;

byte[] b=null;

try {

url = new URL(path); //设置URL

HttpURLConnection con;

con = (HttpURLConnection)url.openConnection(); //打开连接

con.setRequestMethod("GET"); //设置请求方法

//设置连接超时时间为5s

con.setConnectTimeout(5000);

InputStream in=con.getInputStream(); //取得字节输入流

b=StreamTool.readInputStream(in);

} catch (Exception e) {

e.printStackTrace();

}

return b; //返回byte数组

}

}

3. 写一个用户操作界面

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/picaddress"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="http://www.desk9.com/Desk9Image/21/Desk9_21_1690_35790_S.jpg"

android:id="@+id/imageaddress"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/look"

android:id="@+id/button"

/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/image"/>

4. 写一个activity类

package org.lxh.net;

import org.lxh.service.WebService;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.Toast;

public class NetActivity extends Activity {

private EditText picaddress;

private Button button;

private ImageView imageView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button=(Button)this.findViewById(R.id.button);

imageView=(ImageView)this.findViewById(R.id.image);

picaddress=(EditText)this.findViewById(R.id.imageaddress);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String address=picaddress.getText().toString();

try {

byte[] data=WebService.getImage(address); //得到图片的输入流

//二进制数据生成位图

Bitmap bit=BitmapFactory.decodeByteArray(data, 0, data.length);

imageView.setImageBitmap(bit);

} catch (Exception e) {

Log.e("NetActivity", e.toString());

Toast.makeText(NetActivity.this, R.string.error, 1).show();

}

}

});

}

}

5. 添加网络访问的权限

package="org.lxh.net"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name">

6. 这里把strings.xml文件也贴出来

Hello World, NetActivity!

图片查看

图片地址

查看

网络连接异常

下面是运行效果图:

2b74d5dfcf2f0920e9bd15835a43c8d1.png

希望本文所述对大家Android程序设计有所帮助。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐