我正在研究 ImageView,我想在其中将图像设置为 Imageview,但 Image(byte array) 直接来自 Web 服务器。概念是:
1)我向网络服务器发送各种参数,作为回报,网络服务器向我发送该特定 ID 的图像。(只是一个图像,而不是 URL)
2) 现在我想直接将 Image 显示到 ImageView 中而不保存到 SD 卡中,这样每当用户想要查看任何其他 ID 相关的 Image 时,该 Image 应该直接来自服务器,我们最终不会保存它。
[已编辑]
从服务器获取值并在 byte[] 中返回
static BufferedReader in=null;
byte[] result_image;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri1);
if(list!=null)
{
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
httpPost.setEntity(formEntity);
}
//URI uri=httpPost.getURI();
HttpResponse httpResponse = httpClient.execute(httpPost);
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
// String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line +" "); //sb.append(line +NL);
}
in.close();
result_image = String.valueOf(sb).getBytes();
}
catch(UnsupportedEncodingException e)
{
String err = (e.getMessage()==null)?"Cant connect to server":e.getMessage();
Log.e("Network Error:",err);
}
catch (MalformedURLException e) {
String err = (e.getMessage()==null)?"Malformed Exception":e.getMessage();
Log.e("Malformed Exception:",err);
}
catch(Exception ex)
{
// Log.i("Exception,ex", ex.getMessage());
String err = (ex.getMessage()==null)?"NetworkConnectionException":ex.getMessage();
Log.e("NetworkConnectionException:",err);
}
finally {
if (in != null) {
try {
in.close();
} catch (Exception ex) {
String err = (ex.getMessage()==null)?"Excepion":ex.getMessage();
Log.e("Exception:",err);
}
}
这里 result_image 是一个字节 [] 并且使用这个字节 [] 正在对其进行解码并在位图中显示它..请帮助...对此的任何帮助非常感谢..谢谢..