My goal is to download images from a google app engine server with my android application. I use for that an asynctask. The server is located in google app engine and the images (JPEG files) are in blobstore. First I checked whether the server is working with my google chrome browser. I could see the picture with the same URI on my browser. So my server is working fine. My connection has a session and it is a https connection. My app should download images as drawable (or bitmap?) and make an array from them and put this array into an imageadapter for a gallery. The problem is that when I do Drawable myImage = Drawable.createFromStream(is, “Image”); myImage is null. (I check this with an If (myImage==null)). Why is my browser showing the image but it is null in my code?
PS: I made a lot of search but there are lots of alternatives for downloading image from web. But I can’t understand why this isn’t working. I ran the code on android 2.2.
Here is the code:
URI uri = new URI(URIString);
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
HttpResponse response = httpclient.execute(httpget, myLibrary.localContext);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Drawable myImage= Drawable.createFromStream(is, "Image");
entity.consumeContent();
if (myImage!=null){
System.out.println("PhotoID " + PhotoID + " is NOT null!");
return myImage;
} else {
System.out.println("PhotoID " + PhotoID + " is null!");
return null;
}
Thanks for any idea!
You should check the status of the response before attempting to parse the response entity. The HTTP status of the response is the first place to check for errors. It can be checked with
response.getStatusLine().getStatusCode(). If it is not200then there’s a problem with your HTTP request, and that is causing your problem.