我的问题是:我正在尝试将byte []转换为图像。 byte []来自JSON,它采用以下格式:
“4oCwUE5HDQoaCAAAAA1JSERSAAAAfAAAAFAIBAAAADBHwqrDsAAAAAlwSFlzAAAAJwAAACcBKgnigJhPAAAgAElEQVR4xZPCrMK9ecWSZcOZfcOfw7c5w5vCvW / CqXp ...”它还有100行。
出现问题的代码:
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);
}
在我在decodeByte(Bitmap)获取NULL值之前,它告诉我:
无法解码流:FileNotFoundException。
我已经在这两天挣扎了,不能让这个工作!
任何可能出错的想法?
[编辑]这些是来自debbuger的值:
jsonString“[{”id“:”16“,”clubid“:”1“,”name“:”Theo 沃尔科特 “ ”密码“: ”0000“, ”形象“: ”4oCwUE5HDQoaCgAAAA1JSERS ... WeKAsGfDngAAAABJRU5Ewq5CYOKAmg ==“, ”LASTUPDATED“:” 2013年8月22日 09:31:58“,”isDeleted“:”0“}]”(id = 830043725448)地图HashMap (id = 830043562472)名称“Theo Walcott”(id = 830043434760)
参数ArrayList(id = 830043725168)arg0 String [0] (id = 830043671992)stream ByteArrayOutputStream(id = 830043562528)
decodeString(id = 830045128536)图像“‰PNG \ r \ n \ n
LogCat:
10-30 14:02:57.717:D / JSON对象:(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):无法解码流: java.io.FileNotFoundException:/:open failed:EISDIR(是一个目录) 10-30 14:03:03.747:I / System.out(14452):resolveUri失败了 位图uri
答案 0 :(得分:0)
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);
}
这是您应该从json获得的响应中将String
解码为Bitmap
的方法。