android - How to only retrieve a URL once per instance? -
i load images using httpget request gallery view... problem each time gallery swiped images fetched on again.
i catch images using code below... how this?
public void getimages() throws ioexception{ defaulthttpclient httpclient = new defaulthttpclient(); httpget httppost = new httpget("https://sites.google.com/site/theitrangers/images/webimages.txt"); httpresponse response; response = httpclient.execute(httppost); httpentity ht = response.getentity(); bufferedhttpentity buf = new bufferedhttpentity(ht); inputstream = buf.getcontent(); bufferedreader r = new bufferedreader(new inputstreamreader(is)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line + "\n"); imageurl = total.tostring(); log.v("getimage1", "retreived image"); } } public void getimage2() throws ioexception{ defaulthttpclient httpclient = new defaulthttpclient(); httpget httppost = new httpget("https://sites.google.com/site/theitrangers/images/webimage2.txt"); httpresponse response; response = httpclient.execute(httppost); httpentity ht = response.getentity(); bufferedhttpentity buf = new bufferedhttpentity(ht); inputstream = buf.getcontent(); bufferedreader r = new bufferedreader(new inputstreamreader(is)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line + "\n"); imageurl2 = total.tostring(); log.v("getimage2", "retreived image"); } } public void getimage3() throws ioexception{ defaulthttpclient httpclient = new defaulthttpclient(); httpget httppost = new httpget("https://sites.google.com/site/theitrangers/images/webimage3.txt"); httpresponse response; response = httpclient.execute(httppost); httpentity ht = response.getentity(); bufferedhttpentity buf = new bufferedhttpentity(ht); inputstream = buf.getcontent(); bufferedreader r = new bufferedreader(new inputstreamreader(is)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line + "\n"); imageurl3 = total.tostring(); log.v("getimage3", "retreived image"); } } public void getimage4() throws ioexception{ defaulthttpclient httpclient = new defaulthttpclient(); httpget httppost = new httpget("https://sites.google.com/site/theitrangers/images/webimage4.txt"); httpresponse response; response = httpclient.execute(httppost); httpentity ht = response.getentity(); bufferedhttpentity buf = new bufferedhttpentity(ht); inputstream = buf.getcontent(); bufferedreader r = new bufferedreader(new inputstreamreader(is)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line + "\n"); imageurl4 = total.tostring(); } } //imageadapter gets url of images , put them in format set gallery public class imageadapter extends baseadapter { /** parent context */ private context mycontext;public imageadapter() { // todo auto-generated constructor stub } /** url-strings remote images. */ private string[] myremoteimages = {imageurl,imageurl2,imageurl3,imageurl4}; /** simple constructor saving 'parent' context. */ public imageadapter(context c) { this.mycontext = c; } /** returns amount of images have defined. */ public int getcount() { return this.myremoteimages.length; } /* use array-positions unique ids */ public object getitem(int position) { return position; } public long getitemid(int position) { return position; } /** returns new imageview * displayed, depending on * position passed. */ public view getview(int position, view convertview, viewgroup parent) { imageview = new imageview(this.mycontext); try { /* open new url , inputstream load data it. */ url aurl = new url(myremoteimages[position]); urlconnection conn = aurl.openconnection(); conn.connect(); inputstream = conn.getinputstream(); /* buffered performance plus. */ bufferedinputstream bis = new bufferedinputstream(is); /* decode url-data bitmap. */ bitmap bm = bitmapfactory.decodestream(bis); bis.close(); is.close(); log.v(imageurl, "retrieving image"); /* apply bitmap imageview returned. */ i.setimagebitmap(bm); } catch (ioexception e) { log.e("debugtag", "remtoe image exception", e); } /* image should scaled width/height set. */ i.setscaletype(imageview.scaletype.fit_center); /* set width/height of imageview. */ i.setlayoutparams(new gallery.layoutparams(150, 150)); return i; } /** returns size (0.0f 1.0f) of views * depending on 'offset' center. */ public float getscale(boolean focused, int offset) { /* formula: 1 / (2 ^ offset) */ return math.max(0, 1.0f / (float)math.pow(2, math.abs(offset))); } } //the important asynctask method. running background thread images , set them gallery. private class mytask extends asynctask<void, void, void>{ @override protected void doinbackground(void... arg0) {try { getimages(); log.v("mytask", "image 1 retreived"); getimage2(); log.v("mytask", "image 2 retreived"); getimage3(); log.v("mytask", "image 3 retreived"); getimage4(); log.v("mytask", "image 4 retreived"); } catch (ioexception e) { log.e("mainmenu retreive image", "image retreival failed"); e.printstacktrace(); } return null; } @override protected void onpostexecute(void notused){ ((gallery) findviewbyid(r.id.gallery)) .setadapter(new imageadapter(mainmenu.this)); } } }
also how go displaying progress spinner in view gallery while images loading? dont want spinner block ui
how using flag
private boolean llget1 = false; private boolean llget2 = false; private boolean llget3 = false; private boolean llget4 = false; @override protected void doinbackground(void... arg0) {try { if(llget1==false){ getimages(); llget1=true; } if(llget2==false){ log.v("mytask", "image 1 retreived"); getimage2(); llget2=true; } if(llget3==false){ log.v("mytask", "image 2 retreived"); getimage3(); llget3=true; } if(llget4==false){ log.v("mytask", "image 3 retreived"); getimage4(); llget4=true; } log.v("mytask", "image 4 retreived"); } catch (ioexception e) { log.e("mainmenu retreive image", "image retreival failed"); e.printstacktrace(); } return null; }
Comments
Post a Comment