【问题标题】:Set Bitmap on ImageView - Failed to create image decoder with message 'unimplemented'在 ImageView 上设置位图 - 无法创建带有消息“未实现”的图像解码器
【发布时间】:2020-06-20 21:09:51
【问题描述】:

我有一个带有两种方法的 Spring Api:一种方法返回 List 作为 JSONArray,另一种方法返回 byte[]。

当我调用 Volley 多部分请求返回一个 byte[] 时,我可以在 ImageView 上设置它。

API

@GetMapping(value = "/return", produces = MediaType.IMAGE_JPEG_VALUE)
    public @ResponseBody byte[] getImage() throws IOException {     
        File myFile = new File(xxxxxxxxxxxxxxxxxxxxxxx);
        InputStream input = new FileInputStream(myFile);
        System.out.println(input);      
        return IOUtils.toByteArray(input);    
    }

应用程序

    public void returnImage(final ImageView im) {

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        BinaryRequest binaryRequest = new BinaryRequest(0, getString(R.string._url),
            new Response.Listener<byte[]>() {
                @Override
                public void onResponse(byte[] response) {

                    im.setImageBitmap(BitmapFactory.decodeByteArray(response, 0, response.length));

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }
        );
        requestQueue.add(binaryRequest);
    }

当我调用 JSONArrayRequest 时,我无法获取字符串、转换为字节并在 ImageView 上设置。

API上的代码基本相同,但返回不同。

示例返回:

[
    {
        "images": [],
        "JSON": {
            "XX":XX,
            "XX":XX,
            "XX":XX,
            "XX":XX,
            "XX":XX,
            "XX":XX,
            "XX":XX,
            "XX":XX,
        }
    }
]

应用程序

JSONArray jsa = jsonArrayResponse.getJSONObject(i).getJSONArray("images");

if(jsa != null){

    for (int nbr = 0; nbr < jsa.length(); nbr++) {

        byte[] bytes = jsa.get(nbr).toString().getBytes();

        ivPhoto.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));

        return;

    }

}

(我现在只收到一张图片)。

我得到了 String 的返回值并穿上了 https://codebeautify.org/base64-to-image-converter。 图像显示正确。

【问题讨论】:

    标签: android spring-boot imageview android-volley bitmapfactory


    【解决方案1】:

    据我了解,您会收到 Base64 格式的图片。任何字符都可以表示为字节,但这并不意味着您可以将其视为图像。基本上发生的是,您尝试从以 Base64 格式编码的图像创建图像,而 BitmapFactory.decodeByteArray 需要

    @param data 压缩图像数据的字节数组

    在将字节解码为图像之前,您必须先从 Base64 解码为字节。

    byte[] decodedImageBytes = Base64.decode(encodedImage, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(decodedImageBytes, 0, decodedImageBytes.length);
    

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多