如何在android的sqlite的显示从BLOB数据的图像?图像、数据、如何在、sqlite

由网友(温柔败给教训)分享简介:我一直在尝试存储图像格式的Andr​​oid SD卡的SQLite数据库。它工作得很好。该图像是越来越存储到数据库中作为斑点。这是一个粗糙code我一直在使用了点。I have been trying to store image form android sdcard to the sqlite database....

我一直在尝试存储图像格式的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);

Android 开发中 Mac 解决 SQLite 连接问题

和用于显示我已经使用了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.

阅读全文

相关推荐

最新文章