从webservice下载字节数组中的图像并显示它时遇到问题

时间:2014-04-22 11:38:23

标签: android bitmap

我试图以字节数组的形式从webservice中获取一个图像然后在屏幕上显示它,同时将字节数组转换为位图我得到了null值。有谁可以帮我如何继续

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;

}

1 个答案:

答案 0 :(得分:0)

您无法直接从endcode imagestring创建Bitmap。首先,您需要使用Base64解码对其进行解码,然后生成如下位图:

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;

    }