如何在android sqlite中显示来自blob数据的图像?

时间:2011-05-17 09:20:42

标签: android image sqlite

我一直在尝试将图像形式的android sdcard存储到sqlite数据库中。它工作得很好。图像以blob形式存储在数据库中。这是我一直在使用的粗略代码。


             Bitmap bitmap = BitmapFactory.decodeFile(filePath);
     imgView.setImageBitmap(bitmap);
     int size = bitmap.getWidth() * bitmap.getHeight();
     ByteArrayOutputStream out = new ByteArrayOutputStream(size);
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
     try {
     out.flush();
     out.close();
     } catch (IOException e) {
     e.printStackTrace();}
             byte[] x = out.toByteArray();

在数据库方面,代码就像这样

          ContentValues values = new ContentValues();
          byte[] bytes = null;
          bytes = img_bytes.getBytes();
          values.put("IMAGE", bytes);

为了显示,我使用了以下代码。

    ImageView image = new ImageView(this);
    byte[] img_bytes = result.get("IMAGE").getBytes();
    Bitmap bMap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length);
image.setImageBitmap(bMap);

当我打印字节数组的字符串值时,它变得像[B @ 44da2db0。但是字节数组没有显示在图像中。

有人请帮帮我。 感谢

1 个答案:

答案 0 :(得分:3)

我找到了解决方案。谢谢大家。

我使用过base64编码方法。 即;为了显示图像,我一直在使用哈希映射来首先从数据库表中获取结果。 因此,在保存到地图之前,我已使用base 64方法将字节编码为字符串。 然后在活动侧显示图像,我再次使用base 64解码回字符串,然后转换为字节并显示图像。

相关问题