简体   繁体   中英

How to get the image from parsed JSON into ListView

My problem is: i'm trying to convert byte[] into image. The byte[] comes from JSON and it's in this format:

"4oCwUE5HDQoaCgAAAA1JSERSAAAAfwAAAFAIBgAAADBHwqrDsAAAAAlwSFlzAAAAJwAAACcBKgnigJhPAAAgAElEQVR4xZPCrMK9ecWSZcOZfcOfw7c5w5vCvW/CqXp..." it goes for another 100 lines.

The code where the problem occurs:

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);
            }

Before I was getting NULL value at decodedByte (Bitmap) now it's telling me:

Unable to decode stream: FileNotFoundException.

I'm struggling with this two days already and can't get this working!

Any ideas of what might be wrong?

[EDIT] These are values from the debbuger:

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

And the LogCat:

10-30 14:02:57.717: D/JSON OBJECTS:(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): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory) 10-30 14:03:03.747: I/System.out(14452): resolveUri failed on bad bitmap uri

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);
}

This is how you should decode a String to a Bitmap from the response you get from json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM