您的位置:首页 > 其它

Bitmap和ImageView显示下载的图片(I/O的理解)

2012-11-18 01:54 405 查看
本篇博文Deprecated 最新文章请参见 I/0流的梳理和小结

1 利用图片的路径,得到图片的字节数组

public static byte [] getImage(String path) throws Exception{
URL imageUrl=new URL(path);
HttpURLConnection connection=(HttpURLConnection)imageUrl.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
byte [] imageData=GetResource.readResource(inputStream);
return imageData;
}
return null;
}


分析:
(1)利用URL得到HttpURLConnection connection这样便于资源建立起了联系,且设置connection的属性值
(2)利用HttpURLConnection connection得到输入流.即可以这么想:此时的图片已经保存到了此输入流inputStream里
(3)将在输入流里的图片数据输出到字节数组里面.即byte [] imageData=GetResource.readResource(inputStream).如下

readResource(inputStream)方法如下:

public static byte[] readResource(InputStream inputStream) throws Exception{
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte [] buffer=new byte[1024];
int len=0;
while( (len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
inputStream.close();
outputStream.close();
return outputStream.toByteArray();
}


分析:
(1)没有方法可以把输入流里的数据直接放到字节数组里(这句说法不太准确哈),而是要利用ByteArrayOutputStream outputStream
把在输入流自己把自己的数据读(read())到一个字节数组里面,即inputStream.read(buffer),然后数组里面的数据放入
输出流ByteArrayOutputStream outputStream里面,即outputStream.write(buffer,0,len);
(2)待数据全部转移到输入流outputStream里面,此时就可以把输出流的数据全部转换为字节数组,即outputStream.toByteArray();
(3)在此例子就很好体现了输入流和输出流的使用.
在输入流相应的API中都是把输入流读取到一个数组中,或者只读取一个字节,或者读取一行
如FileInputStream类中的方法:
public int read(byte[] b,int off,int len)从此输入流中将最多 len 个字节的数据读入一个字节数组中
public int read()从此输入流中读取一个数据字节
如在BufferedReader类中的方法:
public String readLine() 读取一个文本行.返回值:包含该行内容的字符串

在输出流相应的API中都把是字节数组写入此输出流,或者只把数组中的某个位置的数据写入输出流
如ByteArrayOutputStream类的方法中:
public void write(byte[] b,int off,int len)将指定字节数组中从偏移量off开始的len个字节写入此字节数组输出流
public void write(int b)将指定的字节写入此字节数组输出流
然后我们可以发现:
(1)可以把输出流里的数据转换为字节数组
如ByteArrayOutputStream类的方法中:
public byte[] toByteArray():创建一个新分配的字节数组。其大小是此输出流的当前大小,并且缓冲区的有效内容已复制到该数组中。
(2)可以把输出流里的数据转换为字符串
如ByteArrayOutputStream类的方法中:
public String toString():将缓冲区的内容转换为字符串,根据平台的默认字符编码将字节转换成字符。

有了上面的铺垫就可以更好地理解照片复制:
package cn.io;
//FileInputStream和FileOutputStream实现照片
//注意图片不可以用字符流(如FileReader和FileWriter)拷贝,因为它会去查找字符表
//在方式二中使用InputStream中的available()方法建立缓冲区
//这样操作的好处是不用循环操作,直接先全部暂存在一个数组里,然后再全部取出存到目的地

import java.io.*;
public class CopyPhoto{
public static void main(String[] args) throws Exception{
FileInputStream fis=null;
FileOutputStream fos=null;
//…………………………………………以下为方式一
fis=new FileInputStream("F:\\1.JPG");
fos=new FileOutputStream("F:\\2.JPG");
byte [] temp1=new byte[1024*1024];
int length=0;
while((length=fis.read(temp1))!=-1){
fos.write(temp1, 0, length);
fos.flush();
}
//…………………………………………以下为方式二
fis=new FileInputStream("F:\\3.JPG");
fos=new FileOutputStream("F:\\4.JPG");
byte [] temp2=new byte[fis.available()];
fis.read(temp2);
fos.write(temp2);

fis.close();
fos.close();
}
}


2 生成位图且利用ImageView进行显示
byte [] imageData=GetImageResource.getImage(path);
Bitmap bitmap=BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
imageView.setImageBitmap(bitmap);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: