如何在android的sqlite的显示从BLOB数据的图像? [英] How to display image from blob data in android sqlite?

查看:89
本文介绍了如何在android的sqlite的显示从BLOB数据的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试存储图像格式的Andr​​oid SD卡的SQLite数据库。它工作得很好。该图像是越来越存储到数据库中作为斑点。这是一个粗糙code我一直在使用了点。

I have been trying to store image form android sdcard to the sqlite database. And it worked fine. The image is getting stored into the database as blob. This is the rough code I have been using for that.

             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();


在数据库端,在code是这样


In the database side, the code is like this

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


和用于显示我已经使用了code如下。


And for displaying I have used the code as below.

    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);

当我打印的byate数组的字符串值,它是越来越像[B @ 44da2db0。但是字节数组是没有得到显示成图像。

When I printed the string value of the byate array, it was getting like [B@44da2db0. But the byte array is not getting displayed into image.

有人请帮助我。 谢谢

推荐答案

我已经找到了解决办法。感谢所有。

I have found a solution. Thanks to all.

我已经使用base64编码方法。 即;对于显示图像,我已经使用哈希映射得到的结果首先从数据库表是。 所以保存到地图之前,我有恩codeD使用基地64法字节字符串。 然后在活动侧,用于显示图像,我有德$ C $光盘回来,然后使用碱64将字符串转换成字节,并且该图像得到显示。

I have used base64 encoding method. ie; For displaying the image, I have been using a hash map to get the result first of all from the database table. So before saving into the map, I have encoded the bytes to string using base 64 method. Then On the Activity side for displaying the image, I have decoded back the string using base 64 again and then converted to bytes and the image got displayed.

这篇关于如何在android的sqlite的显示从BLOB数据的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆