Android开发获取ImageView中的图像

ImageView是一个用于显示图像的控件,比如在很多联系人方面的应用中,需要为联系人拍摄头像,并将得到的头像放在一个ImageView对象中显示。通常,在没有为联系人拍摄头像之前,ImageView对象中显示的是一个缺省的图片
[img]http://dl.iteye.com/upload/attachment/589239/901552f3-c6fd-3ed1-a0fb-b243d79132ed.png[/img]
代码实现

/**
但是需要说明的是:

1. 在调用getDrawingCache()方法从ImageView对象获取图像之前,一定要调用setDrawingCacheEnabled(true)方法:

iconView.setDrawingCacheEnabled(true);

否则,无法从ImageView对象iv_photo中获取图像;

2. 在调用getDrawingCache()方法从ImageView对象获取图像之后,一定要调用setDrawingCacheEnabled(false)方法:

iconView.setDrawingCacheEnabled(false);

以清空画图缓冲区,否则,下一次从ImageView对象iconView中获取的图像,还是原来的图像。

此方法返回byte[] 是为了方便保存到SqlLite中
关于SqlLite中保存图片的字段类型为 image
ContentValues initialValues = new ContentValues();
initialValues.put(Utils.ICON, byte[] data);
*/
private byte[] getDefaultIcon() {
// iconView.get
// BitmapFactory.
byte[] compressData = null;
iconView.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(iconView.getDrawingCache());
iconView.setDrawingCacheEnabled(false);
if (bmp != null) {
compressData=getByteByBitmap(bmp);
}
return compressData;
}

private byte[] getByteByBitmap(Bitmap bmp){
byte[] compressData = null;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
// bmp.
compressData = outStream.toByteArray();
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return compressData;
}

保存到数据库中,读取出来显示到ImageView方法

byte[] data=p.getIcon();
Bitmap bmp=null;
if (data != null && data.length > 0) {
bmp = BitmapFactory.decodeByteArray(data, 0,
data.length);
}

具体参考[url=http://02191986-163-com.iteye.com/admin/blogs/1261561]Android用simpleAdapter来直接显示BMP图片 涉及的系列问题[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值