如何从json对象解码图像?

时间:2013-12-12 06:54:23

标签: android json bitmap

我正在尝试将Base64格式字符串中的图像发布到Web服务器。我需要从Web服务器检索它并以Bitmap格式解码它,以便我可以在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

1 个答案:

答案 0 :(得分:0)

嘿,你想要解码来自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;
    }