转自http://www.linuxidc.com/Linux/2011-07/39106.htm
  1. publicvoidsaveIcon(Bitmapicon){
  2. if(icon==null){
  3. return;
  4. }
  5. //最终图标要保存到浏览器的内部数据库中,系统程序均保存为SQLite格式,Browser也不例外,因为图片是二进制的所以使用字节数组存储数据库的
  6. //BLOB类型
  7. finalByteArrayOutputStreamos=newByteArrayOutputStream();
  8. //将Bitmap压缩成PNG编码,质量为100%存储
  9. icon.compress(Bitmap.CompressFormat.PNG,100,os);
  10. //构造SQLite的Content对象,这里也可以使用raw
  11. ContentValuesvalues=newContentValues();
  12. //写入数据库的Browser.BookmarkColumns.TOUCH_ICON字段
  13. values.put(Browser.BookmarkColumns.TOUCH_ICON,os.toByteArray());
  14. DBUtil.update(....);//调用更新或者插入到数据库的方法
  15. }

方法二:

  1. importAndroid.provider.MediaStore.Images.Media;
  2. importandroid.content.ContentValues;
  3. importjava.io.OutputStream;
  4. //SavethenameanddescriptionofanimageinaContentValuesmap.
  5. ContentValuesvalues=newContentValues(3);
  6. values.put(Media.DISPLAY_NAME,"road_trip_1");
  7. values.put(Media.DESCRIPTION,"Day1,triptoLosAngeles");
  8. values.put(Media.MIME_TYPE,"image/jpeg");
  9. //Addanewrecordwithoutthebitmap,butwiththevaluesjustset.
  10. //insert()returnstheURIofthenewrecord.
  11. Uriuri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,values);
  12. //Nowgetahandletothefileforthatrecord,andsavethedataintoit.
  13. //Here,sourceBitmapisaBitmapobjectrepresentingthefiletosavetothedatabase.
  14. try{
  15. OutputStreamoutStream=getContentResolver().openOutputStream(uri);
  16. sourceBitmap.compress(Bitmap.CompressFormat.JPEG,50,outStream);
  17. outStream.close();
  18. }catch(Exceptione){
  19. Log.e(TAG,"exceptionwhilewritingimage",e);
  20. }

从数据库中读取:

  1. byte[]blob=cur.getBlob(cur.getColumnIndex(KEY_IMG));
  2. Bitmapbmp=BitmapFactory.decodeByteArray(blob,0,blob.length);

更多相关文章

  1. Android(安卓)4.0.1_r1源代码发布,更新获取方法
  2. Android全屏(包含3种隐藏顶部状态栏及标题栏和一种隐藏Android(安
  3. Android中JNI高级应用 - 本地C代码中创建Java对象及本地JNI对象
  4. 动态权限
  5. android中获取string字符串的方法
  6. Android屏幕100%适配方案
  7. java.lang.IllegalStateException Fragment already added: Home
  8. android软件盘的开关
  9. Android面试-Android部分

随机推荐

  1. android 一个app启动另一个App的几种方法
  2. Android调用系统, 任意比例裁剪图片
  3. Android 之ActivityThead、ActivityManag
  4. 获取Android设备上的详细的摄像头信息
  5. ListView setOnItemClickListener无效原
  6. Android笔记二十三.Android基于事件监听
  7. Bitmap and DrawPoint
  8. Android笔试(一)
  9. Android 学习笔记1:基本架构
  10. Android开发入门教程1-初试Android