Facing Issue when downloading image in byte array from webservice and displaying it

StackOverflow https://stackoverflow.com/questions/23218396

  •  07-07-2023
  •  | 
  •  

Question

I am trying to fetch an image from webservice in form of byte array and then display it on screen while converting the byte array to bitmap I am getting null value.Could anyone help me how to proceed

public String FinalOutput(String response) {

    String status = null;
    try {
        JSONObject parse = new JSONObject(response);
        JSONObject registerParse = (JSONObject) parse
                .get("BusinessSearches");
        status = registerParse.get("Image").toString();

        byte[] theByteArray = status.getBytes();

        System.out.println("theByteArray" + theByteArray);

        Bitmap bitmap1 = BitmapFactory.decodeByteArray(theByteArray, 0,
                theByteArray.length);

        System.out.println("bitmap" + bitmap1);
        ApplicationConstant.bitmapValue = bitmap1;

    } catch (Exception e) {
        System.out.println("Error   :::::  " + e.getMessage());
    }

    return status;

}
Was it helpful?

Solution

You cannot create Bitmap from endcode imagestring directly. First you need to decode it using Base64 decoding and then generate the bitmap like this :

public String FinalOutput(String response) {

        String status = null;
        try {
            JSONObject parse = new JSONObject(response);
            JSONObject registerParse = (JSONObject) parse.get("BusinessSearches");
            status = registerParse.getString("Image");

            byte[] decodedString = Base64.decode(status, Base64.DEFAULT);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            ApplicationConstant.bitmapValue = bitmap1;

        } catch (Exception e) {
            System.out.println("Error   :::::  " + e.getMessage());
        }

        return status;

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top