Bitmap与Drawable、byte[]、图片资源、图片文件的转换(二)
来源:互联网 发布:淘宝上的台式机能买吗 编辑:程序博客网 时间:2024/04/30 04:26
1.从资源获取bitmap
使用bitmap工厂的dacodeResource()方法
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.pic);
2.从图片文件获取bitmap
从图片文件获取bitmap后,才能调用ImageView的setImageBitmap(Bitmap bitmap)方法
使用bitmap工厂的dacodeFile()方法
String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath);3.从图片文件按比例获取bitmap
如果图片过大,可能导致Bitmap对象装不下图片
解决办法:
String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); /*将图片的长和宽缩小为原来的1/2*/
private Options getBitmapOption(int inSampleSize){
System.gc();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = inSampleSize;
return options;
}
4.bitmap转为图片文件
从网络下载到的图片数据转为bitmap后,就要转为图片文件保存。
创建文件输出流,调用bitmap对象的compress方法
该方法中,第一个参数是上一级文件夹,第二个参数是文件名
5.btye[]转为bitmap
从网络下载到的图片数据为btye[]字节数组,要将它转为bitmap
我们都知道,字节是计算机中数据处理的基本单位。
使用bitmap工厂的dacodeByteArray()方法
HttpEntity entity = httpResponse.getEntity();
byte[] byteIn = EntityUtils.toByteArray(entity);
BitmapFactory.decodeByteArray(byteIn, 0, byteIn.length);
6.bitmap转为btye[]
当图片上传到服务器时,要用到将bitmap转为btye[]
创建字节数组输出流,调用bitmap对象的compress方法
public byte[] bitmapToBytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
//baos是ByteArrayOutputStream的首字母拼写
同理,fos是FileOutputStream的首字母拼写
- Bitmap与Drawable、byte[]、图片资源、图片文件的转换(二)
- Bitmap与Drawable、byte[]、图片资源、图片文件的转换
- Drawable之图片转换drawable、bitmap、byte[]
- android在处理图片资源的时候,Drawable、Bitmap、byte[]之间的转换
- 安卓图片--Bitmap与Drawable与byte[]与InputStream之间的转换工具类【转】
- 获取系统图片资源 drawable和bitmap之间的转换
- android 获取资源文件 r.drawable中的图片转换为drawable、bitmap
- Android 获取资源文件 r.drawable中的图片转换为drawable、bitmap
- android 获取资源文件 r.drawable中的图片转换为drawable、bitmap
- 图片drawable与bitmap格式互相转换
- Android图片二进制与Bitmap、Drawable之间的转换
- Android图片二进制与Bitmap、Drawable之间的转换
- Android图片二进制与Bitmap、Drawable之间的转换
- Android图片二进制与Bitmap、Drawable之间的转换
- Android图片二进制与Bitmap、Drawable之间的转换
- Android图片二进制与Bitmap、Drawable之间的转换
- 图片ID与drawable、bitmap之间的转换
- Android图片二进制与Bitmap、Drawable之间的转换
- ptmalloc
- 灵魂尽头——小时代电影全记录III(平装)pdf下载
- 并查集之冗余关系
- 1340: A Sample Problem
- Happy Number
- Bitmap与Drawable、byte[]、图片资源、图片文件的转换(二)
- networkx笔记:绘制基本网络图
- onfocus事件
- 1341: String and Arrays
- JAVA基础一大堆0813Web项目
- 选择排序SelectSort
- coj 1342: Double
- 【Effective C++ 3rd 心得、归纳、实践】 Item 12: 拷贝一个对象的所有组成部分
- spoj1182 Sorted bit squence