1.在Java中操作显示网络图片
public class ImageRequest
{
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
//构造一个URL对象
URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");
//使用openConnection打开URL对象
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//使用Http协议,设置请求方式为GET
conn.setRequestMethod("GET");
//设置链接超时异常,5s
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//获取到图片的二进制数据
byte[] data = readInputStream(inStream);
//构造一个文件,保存图片到项目的根目录下
File imageFile = new File("baidu_logo.jpg");
//构造一个文件输出流FileOutputStream
FileOutputStream outStream = new FileOutputStream(imageFile);
//把文件数据写入到输出流
outStream.write(data);
outStream.close();
}
/**
* 从输入流里面得到返回为二进制的数据
* @param inStream 输入流
* @return byte[] 二进制数据
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception
{
//构造一个ByteArrayOutputStream
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//设置一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
//判断输入流长度是否等于-1,即非空
while( (len=inStream.read(buffer)) != -1 )
{
//把缓冲区的内容写入到输出流中,从0开始读取,长度为len
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
return outStream.toByteArray();
}
}
2.Android中显示图片:
(1)ImageShowActivity.java
public class ImageShowActivity extends Activity
{
private static final String TAG = "ImageShowActivity";
private EditText pathText;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到EditText,ImageView与Button
pathText = (EditText) this.findViewById(R.id.urlpath);
imageView = (ImageView) this.findViewById(R.id.imageView);
Button button = (Button)this.findViewById(R.id.button);
//设置Button监听
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//获取EditText里面的地址
String path = pathText.getText().toString();
try
{
//得到图片的二进制数据
byte[] data = ImageService.getImage(path);
//图片处理
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图
//显示图片
imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
//出错的时候提示错误信息
Toast.makeText(ImageShowActivity.this, R.string.error, 1).show();
//Log里面打印错误信息
Log.e(TAG, e.toString());
}
}
});
}
}
(2)编写一个流处理工具类,StreamTool.java
public class StreamTool
{
/**
* 从输入流里面得到返回为二进制的数据
* @param inStream 输入流
* @return byte[] 二进制数据
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception
{
//构造一个ByteArrayOutputStream
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//设置一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
//判断输入流长度是否等于-1,即非空
while( (len=inStream.read(buffer)) != -1 )
{
//把缓冲区的内容写入到输出流中,从0开始读取,长度为len
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
return outStream.toByteArray();
}
}
(3)编写一个图片处理类,打开URL,获取输入流等操作
public class ImageService
{
public static byte[] getImage(String path) throws Exception
{
//构造一个URL对象
URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");
//使用openConnection打开URL对象
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//使用Http协议,设置请求方式为GET
conn.setRequestMethod("GET");
//设置链接超时异常,5s
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//返回图片的二进制数据
return StreamTool.readInputStream(inStream);
}
}
注意别忘记在AndroidManifest.xml文件中添加访问网络的权限:
<uses-permission android:name="android.permission.INTERNET"/>