Jakubee
Jakubee

Reputation: 634

How to display image in imageView from byte[], BitmapFactory.decodeByteArray returns null

This is my first question on stackoverflow.

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:

DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(PlayersListActivity.URL);
        httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(httpEntity);
        // if this is null the web service returned an empty page
        if(httpEntity == null) // response is empty so exit out
            return null;

        String jsonString = EntityUtils.toString(bufferedEntity);

        // 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>();

                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                int id = jsonObject.getInt("id");
                String name = jsonObject.getString("name");
                byte[] images = jsonObject.getString("image").getBytes();

                Bitmap btm = BitmapFactory.decodeByteArray(images, 0, images.length);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                btm.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                Bitmap btm2 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                ImageView image = (ImageView) findViewById(R.id.image);
                image.setImageBitmap(btm2);

                map.put("name", name.toString());
                map.put("image", images.toString());

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

                playersList.add(map);

And ofcourse error that I'm getting in LogCat:

SkImageDecoder:: Factory returned null.
java.lang.NullPointerException

and it points out on this line:

Bitmap btm2 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

I have done reseach but nothing really points on what I'm missing. Any ideas??

Thanks!!

Upvotes: 2

Views: 5262

Answers (2)

Shailesh Lakum
Shailesh Lakum

Reputation: 137

    **Your web service is the UTF-8 decoder**

String str=[Base64];
str = URLDecoder.decode(str, "UTF-8");
ImageView imgView.setImageBitmap(StringToBitMap(str));

public static Bitmap StringToBitMap(String image) {
        try {
            byte[] encodeByte = Base64.decode(image.getBytes(), Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
                    encodeByte.length);
            return bitmap;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 8939

4oCwUE5HDQoaCgAAAA1JSERSAAAAfwAAAFAIBgAAADBHwqrDsAAAAAlwSFlzAAAAJwAAACcBKgnigJhPAAAgAElEQVR4xZ

PCrMK9ecWSZcOZfcOfw7c5w5vCvW/CqXp..." it goes for another 100 lines.

Its seems like its not a byte[] but its a Base64 String

So try like

String base64 = jsonObject.getString("image");
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);

Hope this help you.

Upvotes: 2

Related Questions