【问题标题】:Reading an image as byte array with Android: skImageDecoder::Factory returned null使用 Android 将图像读取为字节数组:skImageDecoder::Factory 返回 null
【发布时间】:2014-12-16 13:07:35
【问题描述】:

我有一个 Spring MVC Web 服务,它以字节数组的形式返回图像。输出为 json 格式。格式为.png 这是图像的代码 sn-p。

BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
                imagem = ((DataBufferByte) img.getRaster().getDataBuffer

()).getData();

当我运行服务器时,这是输出:

[{"id":0,"caminhoMDPI":null,"caminhoHDPI":"C:/Users/Marcos/Pictures/postos/drawable-

mdpi/esso_logo.png","caminhoXHDPI":null,"caminhoXXHDPI":null,"imagem":"AAAAAAAAAAAAAAAAAAAAAAAA

啊啊啊啊啊啊啊啊啊……]

最终,“imagem”字段上会出现其他符号。我想这是对的,但我不是

当然。

在我的 Android 应用程序中,我有一个例程来下载图像并将其作为 blob 存储在数据库中。我收到 json 格式并将其转换为带有 jackson 的类。我已经记录了“imagem”字段,它看起来一样。

我的问题是我无法将其转换为图像。这是sn-p的代码:

byte[] img_bandeira = cursor.getBlob(cursor.getColumnIndex("img_bandeira"));
        Bitmap bmpBandeira = BitmapFactory.decodeByteArray(img_bandeira, 0, img_bandeira.length);
        ImageView ivBandeira = (ImageView) view.findViewById(R.id.ivBandeira);
        ivBandeira.setImageBitmap(bmpBandeira);

我得到的只是一条消息:skImageDecoder::Factory 返回 null。

我查看了其他类似的帖子,尝试更改一些行,但没有任何反应。我不知道这里发生了什么。提前致谢。

【问题讨论】:

    标签: java android spring-mvc jackson


    【解决方案1】:

    我解决了。在服务器中,图像应该这样发送:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
                    ImageIO.write(img, "png", baos);
                    baos.flush();
                    String base64String = Base64.encode(baos.toByteArray());
                    baos.close();
    

    在我的应用程序中,它应该是这样的:

    public static Bitmap getImage(byte[] imageArray) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageArray);
            Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
            return bitmap;
        }
    

    谢谢大家;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 2012-02-29
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多