I am working on ImageView in which I want to set an image into Imageview but that Image(byte array) is coming directly from web server.. The concept is:
1) I am sending various parameteres to web server and in return the web server is sending me an image of that particular ID.(Just an Image, not the URL)
2) Now I want to show that Image directly into ImageView without saving into the SD card, so that whenever the user wants to see any other ID related Image, the Image should come directly from server and we are not saving it at our end.
[Editted]
Fetching value from server and return it in 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);
}
}
Here the result_image is a byte [] and using this byte[] am decoding it and shwing it in Bitmap..Please help... Any help into this is really appreciated.. Thanks..
The decodeByteArray()
method is the suggested way to do this.
If its not working for you, You can decode it manually like the following code.
URL yourl = new URL(src);
HttpURLConnection connection = (HttpURLConnection) yourl.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bis = new BufferedInputStream(input,int);
baf = new ByteArrayBuffer(int);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
Hope the above code shows some alternate ideas to you. Happy coding.