android - Pulling images from Web -
i'm using following code grab images web. uses gridview , depending on position, picks url array. works loading of images hit or miss. every time start app, different number of images load.
it has issues when changing portrait landscape view, 5 out of 10 images may displayed i'll turn device , lose images. few show though.
any ideas on making more robust?
try { urlconnection conn = aurl.openconnection(); conn.connect(); inputstream = conn.getinputstream(); bufferedinputstream bis = new bufferedinputstream(is); bitmap bm = bitmapfactory.decodestream(bis); bis.close(); return bm; } catch (ioexception e) { log.d("debugtag", "error..."); } return null;
one thing read there's known bug decoding bitmaps inputstream, , suggested fix google use flushedinputstream (example in below url):
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
also, i'd put download code asynctask. here's use mine:
public static bitmap loadimagefromuri(uri uri) { url url; try { url = uri.tourl(); } catch (malformedurlexception e) { log.v("url exception", "malformedurlexception"); return null; } try { httpurlconnection connection = (httpurlconnection)url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); return bitmapfactory.decodestream(new flushedinputstream(input)); } catch (ioexception e) { e.printstacktrace(); return null; } }
i pass in uri due way i've got rest of code set up, pass in url instead , skip first part. keep download tying ui thread.
Comments
Post a Comment