【问题标题】:android storing blob in sqlite and retrieving it [duplicate]android在sqlite中存储blob并检索它[重复]
【发布时间】:2013-07-21 03:51:45
【问题描述】:

我将图像作为字节数组存储在一个项目的 sqlite 数据库中。 然后我在另一个项目中使用预先填充的表格

但问题是位图工厂的结果为空。

图片存储代码:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), arr[i]);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
b = stream1.toByteArray();

图片检索代码:

BitmapFactory.Options options = new BitmapFactory.Options();
decodedByte = BitmapFactory.decodeByteArray(Image, 0,Image.length, options);
System.out.println("Image = " + Image);
System.out.println("decodedByte = " + decodedByte);

此处 Image 返回长度为 12 的 byteArray(Image)。但 bitmap(decodedByte) 返回 null 值。

我尝试了很多方法,但找不到解决方案。请帮忙!

【问题讨论】:

  • 代码看起来没问题,你确定你在数据库中存储/检索正确的数据吗?长度 12 远远不足以存储图像,如果无法解码数据,BitmapFactory 将返回null。我想这就是正在发生的事情。
  • @zapl :是的,你是对的!在我存储长度之前是 72219。检索后它只有 12。我该怎么办?

标签: android image bytearray blob


【解决方案1】:

这是我从位图到字符串的函数

    public String BitMapToString(Bitmap bitmap) {
    try {
        Bitmap encogida = Bitmap.createScaledBitmap(bitmap, 226, 180, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        encogida.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        return org.kobjects.base64.Base64.encode(b);
    } catch (Exception ex) {
        Conexiones.escribirLog(Log.getStackTraceString(ex),
                getString(R.string.versionReal));
        Log.e("ERROR BitMapToString ", ""); // ex.getMessage());
        return "";
    }
}

希望对你有帮助

【讨论】:

  • 谢谢,但我正在处理字节数组到位图而不是字符串..
【解决方案2】:

你试过没有 options 参数的 decodeByteArray 吗?我认为在这种情况下它没有用。

无论如何,最好将图像 URI 存储为字符串而不是 sqlite 数据库中的字节数组。

获取 URI:

        String path = Images.Media.insertImage(getContentResolver(), mylogo,
                "title", null);

        Uri uriMyLogo = Uri.parse(path);

        String uriString = uriMyLogo.toString() ;

并从 URI 中获取图像

                Uri uriLogo = Uri.parse(uriString);

                Bitmap logo = null;
                try {
                    logo = Media.getBitmap(this.getContentResolver(), uriLogo);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

如果您无法按自己的意愿管理它,我希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-12
    • 2015-11-21
    • 2014-03-27
    • 2017-09-23
    • 1970-01-01
    • 2011-04-08
    • 2011-11-11
    相关资源
    最近更新 更多