0

我的问题是:我正在尝试将 byte[] 转换为图像。byte[] 来自 JSON,格式如下:

"4oCwUE5HDQoaCgAAAA1JSERSAAAAfwAAAFAIBgAAADBHwqrDsAAAAAlwSFlzAAAAJwAAACcBKgnigJhPAAAgAElEQVR4xZPCrMK9ecWSZcOZfcOfw7c5w5vCvW/CqXp..." 它还有 100 行。

出现问题的代码:

String jsonString = EntityUtils.toString(httpEntity);

        // again some simple validation around the returned string
        if(jsonString != null && jsonString.length() != 0) // checking string returned from service to grab id
        {
            JSONArray jsonArray = new JSONArray(jsonString);
            for(int i=0; i< jsonArray.length(); i++)
            {   
                HashMap<String, Object> map = new HashMap<String, Object>();
                // you need to store the results somewhere and pass it on to the player list
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                int id = jsonObject.getInt("id");
                String name = jsonObject.getString("name");
                byte[] image = jsonObject.getString("image").getBytes();
                String base64 = jsonObject.getString("image");
                try {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();  
                    byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
                    String images = new String(decodedString);
                    Bitmap decodedByte = Utils.getBitmapFromString(images);
                    decodedByte.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    Log.d("DECODEDBYTE IS: ", decodedByte.toString());
                    if (decodedByte != null) {
                        ImageView imgView = (ImageView) findViewById(R.id.image);
                        imgView.setImageBitmap(decodedByte);
                    }
                } catch (Exception e) {
                    Log.d("Exception", e.toString());
                }

                map.put("name", name.toString());
                map.put("image", image);
                Log.d("JSON OBJECTS:", jsonObject.toString());
                Log.d("WHATS IN MAP:", map.toString());

                playersList.add(map);
            }

在我在解码字节(位图)获得 NULL 值之前,它告诉我:

无法解码流:FileNotFoundException。

我已经为此苦苦挣扎了两天,无法正常工作!

有什么想法可能是错的吗?

[编辑] 这些是来自调试器的值:

jsonString "[{"id":"16","clubid":"1","name":"Theo Walcott","password":"0000","image":"4oCwUE5HDQoaCgAAAA1JSERS...WeKAsGfDngAAAABJRU5Ewq5CYOKAmg==" "lastupdated":"2013-08-22 09:31:58","isDeleted":"0"}]" (id=830043725448) 映射 HashMap (id=830043562472) 名称 "Theo Walcott" (id=830043434760)
参数 ArrayList (id=830043725168) arg0 String[0] (id=830043671992) stream ByteArrayOutputStream (id=830043562528)
decodedString (id=830045128536) images "‰PNG\r\n\n

和 LogCat:

10-30 14:02:57.717: D/JSON 对象:(14452): {"id":"24","image":"4oCwUE5HDQoaCgA...CFcKpD8WTw5 10-30 14:02:57.717: D/WHATS IN MAP:(14452): {image=[B@428e6d20, name=Ryo Miyaichi} 10-30 14:03:03.747: E/BitmapFactory(14452): 无法解码流: java.io.FileNotFoundException: /: 打开失败:EISDIR(是一个目录)10-30 14:03:03.747:I/System.out(14452):resolveUri 在错误的位图 uri 上失败

4

1 回答 1

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

if(bitmap != null){
   ImageView imgView = (ImageView) findViewById(R.id.image);
   imgView.setImageBitmap(bitmap);
}

这就是您应该如何从从 json 获得的响应中将a 解码String为 a的方式。Bitmap

于 2013-10-30T13:59:00.990 回答