Android SQLite存取图像

Android SQLite存取图像的简单方法如下:

//Bitmap to byte[]
  public byte[] bmpToByteArray(Bitmap bmp){
      //Default size is 32 bytes
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try {
	    bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
	    bos.close();
	  } catch (IOException e) {
	     e.printStackTrace();
	    }
	 return bos.toByteArray();
  }
  //Cursor to bitmap
  Bitmap cursorToBmp(Cursor c, int columnIndex) {
        byte[] data = c.getBlob(columnIndex);
        try {
            return BitmapFactory.decodeByteArray(data, 0, data.length);
        } catch (Exception e) {
            return null;
        }
 }

图像存储调用:

ContentValuesvalues=newContentValues();

values.put("img",bmpToByteArray(bmp);

图像读取调用:

Cursorc=db.rawQuery("select*frominfo",null);

c.moveToLast();

Bitmapbmp=cursorToBmp(c,c.getColumnIndex("img"));

相关推荐