ImageView是一个用于显示图像的控件,比如在很多联系人方面的应用中,需要为联系人拍摄头像,并将得到的头像放在一个ImageView对象中显示。通常,在没有为联系人拍摄头像之前,ImageView对象中显示的是一个缺省的图片
[img]http://dl.iteye.com/upload/attachment/589239/901552f3-c6fd-3ed1-a0fb-b243d79132ed.png[/img]
代码实现
保存到数据库中,读取出来显示到ImageView方法
具体参考[url=http://02191986-163-com.iteye.com/admin/blogs/1261561]Android用simpleAdapter来直接显示BMP图片 涉及的系列问题[/url]
[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]