Reputation: 10708
I have research alot on this topic, but no clue ..
I am downloading an image from web services but I have to pass post parameters with URL to download specific image only..
Even I don't know the format of Image, but while using AppTester, when I am passing post parameteres value with the URL, the the reponse I am getting thourgh web services is "image.png"
The code that I am trying here is:
public String HTTPConnect(String uri1,List<NameValuePair> list,Context context)
{
try {
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);
// Log.i("RESPONSE RETURNS THIS :", ""+httpResponse);
// Log.i("getEntity().getContent() RETURNS THIS :", ""+httpResponse.getEntity().getContent());
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 = sb.toString();
}
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);
}
}
}
return result;
}
and on the another class I am calling this method and convert the result String into bytes as :
ArrayList<NameValuePair> postParameters2 = new ArrayList<NameValuePair>();
postParameters2.add(new BasicNameValuePair("Token", "token"));
postParameters2.add(new BasicNameValuePair("Action", "GetThumb"));
Bitmap bMap=null;
String CustomerImgXml=HTTPConnect("URL", postParameters2, this);
bMap=BitmapFactory.decodeByteArray(CustomerImgXml.getBytes(), 0, CustomerImgXml.length());
Please somebody help.. I am very confused here
Upvotes: 1
Views: 2473
Reputation: 10708
Finally I got the answer on my own :
I have done the coding as:
public InputStream HTTPImage(String uri1,List<NameValuePair> list) throws Exception
{
InputStream input=null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri1);
Log.i("postParameter,list", ""+list);
if(list!=null)
{
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
httpPost.setEntity(formEntity);
}
HttpResponse httpResponse = httpClient.execute(httpPost);
input=(InputStream) httpResponse.getEntity().getContent();
return input;
}
and In another class: I am using it like :
InputStream in=null;
in=con2.HTTPImage("https://hd.picbusiness.com/icmdev/hhw/App/", postParameters2);
Bitmap bMap=BitmapFactory.decodeStream(in);
img.setImageBitmap(bMap);
Log.i("bMap", ""+bMap);
Thanks everyome for ur support :)
Upvotes: 0
Reputation: 225
Try this code
Bitmap myBitmap;
try {
url = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/0/01/Sachin_Tendulkar.jpg/250px-Sachin_Tendulkar.jpg");
connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
connection.setReadTimeout(120000);
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
ImageView.setImageBitmap(myBitmap);
Upvotes: 1