如何从 JSON 对象中解码图像?

4
我将尝试将图像以Base64格式的字符串发布到Web服务器。我需要从Web服务器检索它并解码为位图格式,以便我可以在listview中显示图像。但是,当我解码时出现错误。请看一下我在此处发布的代码。
编码:
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSION_QUALITY, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
String encodedImage = MyBase64.encode(b);

解码:

byte[] byteArray =  Base64.decode(jsonObject.getString("imageData"), Base64.DEFAULT) ;
System.out.println("byte[]:"+byteArray);
Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

以下是日志记录: ![logcat中显示的错误]

12-12 06:09:12.243: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@40cd85a8: open failed: ENOENT (No such file or directory)
12-12 06:09:12.243: I/System.out(1037): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40cd85a8
12-12 06:09:12.313: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@40ce3620: open failed: ENOENT (No such file or directory)
12-12 06:09:12.313: I/System.out(1037): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40ce3620
12-12 06:09:12.393: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@40ef8fc0: open failed: ENOENT (No such file or directory)
12-12 06:09:12.393: I/System.out(1037): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40ef8fc0
12-12 06:09:12.473: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@40cf2588: open failed: ENOENT (No such file or directory)
12-12 06:09:12.473: I/System.out(1037): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40cf2588
12-12 06:09:12.533: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@40cd6060: open failed: ENOENT (No such file or directory)
12-12 06:09:12.533: I/System.out(1037): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40cd6060
12-12 06:09:12.613: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@40eefc68: open failed: ENOENT (No such file or directory)
12-12 06:09:12.613: I/System.out(1037): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40eefc68

这个链接很有用 https://dev59.com/r3jZa4cB1Zd3GeqPhbvB - Renjith Krishnan
你在解码或编码时遇到了这个错误吗? - M-Wajeeh
@M-WaJeEh,我在解码时遇到了这个错误,请帮帮我。 - user2879697
@RenjithKrishnan 你好,谢谢提供链接。但是对我来说没有用,请指导我如何解决这个错误。 - user2879697
1个回答

1

你想要从JSON对象中解码图像。

首先将图像URL存储在数据库中,并通过JSON访问。

然后从URL获取位图图像。

使用以下代码:

try {
        URL url = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

我认为这是一个循环。 - naty1982

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接