0

我有一张通过网络服务以 JSON 格式发送给我的图像。这是它的外观片段: {"notif_detailsResult":[{"image":[255,216,255,224,0,16,74,....
我想在 Android 的 Imageview 上显示图像。解析 json 后,我可以将图像值存储在字符串中

@Override
protected String doInBackground(String... params) {

String epc = params[0];
String result = null;
String url = "http://192.168.142.1:90/Service1.svc/notif?notif_id=24";
try {

BufferedReader inStream = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpR = new HttpGet(url);
httpR.setHeader("Accept", "application/json");
httpR.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httpR);
System.out.println("HERE in product display after exectunig response");
inStream = new BufferedReader(new     InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while((line = inStream.readLine()) != null)
{
sb.append(line + NL) ;   
}
inStream.close();
result = sb.toString();

} catch(Exception e){
e.printStackTrace();
}
return result;
}

@Override
protected void onPostExecute(String result) {

super.onPostExecute(result);
try {

JSONObject jObject = new JSONObject(result);

JSONArray array = jObject.getJSONArray("notif_detailsResult");
JSONObject jObject1 = array.getJSONObject(0);


String data = jObject1.getString("image");
4

2 回答 2

0

做类似的事情 - 你在“数据”变量中有图像吗?所以现在将你的字符串解析成字节 []..

byte[] outImage=data;//do required thing to parse into byte if needed
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
imgView.setImageBitmap(theImage);
于 2013-07-29T13:24:38.367 回答
0

如果是 byte[],那么您可以将图像值作为对象。

  byte[] b_Image=jsonObject.get("image");       
    image.setImageBitmap(BitmapFactory.decodeByteArray(b_Image,0, b_Image.length));

试试这个。

于 2013-07-29T13:24:39.977 回答