如何在byteView中的imageView中显示图像,BitmapFactory.decodeByteArray返回null

时间:2013-10-29 13:53:54

标签: android json bytearray

这是我关于stackoverflow的第一个问题。

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

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

出现问题的代码:

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

我在LogCat中遇到的错误:

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

并指出这一行:

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

我已经做过研究,但没有什么能指出我所缺少的东西。 有任何想法吗??

谢谢!

2 个答案:

答案 0 :(得分:2)

4oCwUE5HDQoaCgAAAA1JSERSAAAAfwAAAFAIBgAAADBHwqrDsAAAAAlwSFlzAAAAJwAAACcBKgnigJhPAAAgAElEQVR4xZ

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

似乎 一个byte[]但它的Base64 字符串

请尝试

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

希望这对你有所帮助。

答案 1 :(得分:1)

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