sqlite 存储图片

一、bitmap保存到SQLite 中 数据格式:    express

Java代码   收藏代码
  1. db.execSQL("Create table express ( _id INTEGER PRIMARY KEY AUTOINCREMENT,express_no varchar(100),express_name TEXT,express_img BLOB );");  

 

二、bitmap 变为 Blob   缓存

Java代码   收藏代码
  1. ContentValues values = new ContentValues();  
  2. Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.erweima);
  3. final ByteArrayOutputStream os = new ByteArrayOutputStream();   
  4. bmp.compress(Bitmap.CompressFormat.PNG, 100, os);    
  5. values.put("express_img", os.toByteArray());  
  6. values.put("express_name","zf");  
  7. values.put("express_no","zf");  
  8. getContentResolver().insert("express", values);  

 

 三、从SQLite中读取Bitmap    spa

Java代码   收藏代码
  1. byte[] in=cur.getBlob(cur.getColumnIndex("express_img"));  
  2. bmpout=BitmapFactory.decodeByteArray(in,0,in.length);  

 显示在ImageView上code

Java代码   收藏代码
  1. ImageView imageView = (ImageView) view.findViewById(R.id.img);   
  2. ByteArrayInputStream stream = new ByteArrayInputStream(cur.getBlob(cur.getColumnIndex("express_img")));   
  3. imageView.setImageDrawable(Drawable.createFromStream(stream, "img"));   

 

总结:orm

inputStream:  做为数据缓存,数据写如何供别的对象读取,其方法为read();对象

outputStream:做为数据缓存,未来向别的对象写内容!其方法write();get

Java代码   收藏代码
  1. byte[] in=cur.getBlob(cur.getColumnIndex(MyUser.User.BITMAP_VALUES));  

 //这样也能够对数据进行初始化,byte是基本类型,不须要以前进行长度定义。input

相关文章
相关标签/搜索