Bitmap和ImageView显示下载的图片(I/O的理解)
来源:互联网 发布:toyama钢琴如何 知乎 编辑:程序博客网 时间:2024/05/21 10:57
本篇博文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);
- Bitmap和ImageView显示下载的图片(I/O的理解)
- 显示下载的图片的imageview
- ImageView显示本地和网上的图片
- Android 改变ImageView图片的Bitmap大小
- 图片显示 ImageView 要和显示的图一样大小
- android——获取ImageView上面显示的图片bitmap对象
- imageview显示图片的部分
- ImageView的圆形图片显示
- 举例说明android中Bitmap、ListView以及ImageView的综合使用---显示图片并获取图片的宽和高
- ImageView,Bitmap的浅析
- Android手机开发:图片的放大和缩小显示ImageView
- Android用ImageView显示本地和网上的图片
- 【Android】用ImageView显示本地和网上的图片
- Android用ImageView显示本地和网上的图片
- Android用ImageView显示本地和网上的图片
- Android用ImageView显示本地和网上的图片
- Android用ImageView显示本地和网上的图片
- 可以播放GIF和显示圆形图片的自定义ImageView
- (转)高斯平滑或者说滤波器
- 64位vs2010的opencv配置和图片读取问题
- UVa 11402 - Ahoy, Pirates!
- XML的解析和生成
- Tomcat小问题
- Bitmap和ImageView显示下载的图片(I/O的理解)
- Wix学习(三)
- Android查看网页源码
- Sqlite数据库相关操作
- 大小写互换
- 初学WCF时发现的一种调试技巧
- hdu 1055 Color a Tree
- LeetCode Longest Valid Parentheses
- 学习openGL灯光、视角,便于理解参数含义的几个演示程序