Android BitmapFactory can't decode inputstream -
i'm trying image webserver this:
@override protected inputstream doinbackground(string... strings) { inputstream stream = null; try { url url = new url(strings[0]); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.connect(); stream = connection.getinputstream(); connection.disconnect(); } catch (malformedurlexception e) { e.printstacktrace(); } { return stream; } } @override protected void onpostexecute(inputstream stream) { try { bufferedinputstream buf = new bufferedinputstream(stream); bitmap avatar = bitmapfactory.decodestream(buf); user.avatar = avatar; if (user.avatar != null) imgavatar.setimagebitmap(user.avatar); buf.close(); stream.close(); } catch (ioexception e) { e.printstacktrace(); } }
but bitmap avatar
null. i've been looking through received inputstream via debugger, holds right url, , has fields httpengine/responsebodyin/bytesremaining
holding number of bytes equals image size. image in .png format.
ok, solved using apache httpclient instead of httpurlconnection:
protected inputstream doinbackground(string... strings) { httpresponse response = null; inputstream instream = null; try { httpclient client = new defaulthttpclient(); httpget request = new httpget(new url(strings[0]).touri()); response = client.execute(request); if (response.getstatusline().getstatuscode() != 200) { return null; } bufferedhttpentity bufhttpentity = new bufferedhttpentity(response.getentity()); instream = bufhttpentity.getcontent(); return instream; } catch (exception ex) { return null; } { if (instream != null) { try { instream.close(); } catch (ioexception e) { } } } } @override protected void onpostexecute(inputstream stream) { bitmap avatar = bitmapfactory.decodestream(stream); if (avatar != null) { user.avatar = avatar; imgavatar.setimagebitmap(avatar); } }
Comments
Post a Comment