android - Implementing a GalleryAdapter getCount that fetches its content from a remote server -
this looong explanation, please bear me... here dragons...
ok have android gallery , implemented galleryadapter creates views , stuff:
public class galleryadapter extends baseadapter {.... .... etc ...
now, before start, of examples out there show how assume know how many images there going in gallery. in case, not. arbitrary number of images @ least there's consistent naming convention 001, 002, 003 etc
i have folder somewhere on server has images in should displayed in gallery:
http://www.somedomain.com/content/pictures
in pictures folder might have
0001.jpg
0002.jpg
0003.jpg
000n.jpg
etc ad infinitum
now baseadapter extending, expects override public int getcount()
if know have 20 images go:
public int getcount() {return 20;}
and on merry way. case?
way have gotten around till requiring count.txt file placed in root of directory images contains integer tells there x amount of images in it. remember, http location.
so getcount looks more this
public int getcount() { int count = getremoteimagecount(); if(count == -1) { return 0;//oh crap, went wrong, now?! } else { return count; } public int getremoteimagecount() try { url url = new url(strurl); urlconnection conn = url.openconnection(); conn.setusecaches(true); conn.connect(); inputstream instream = conn.getinputstream(); inputstreamreader reader = new inputstreamreader(instream); string strcount = new string(); int data = reader.read(); while(data != -1){ char thechar = (char) data; strcount += thechar; data = reader.read(); } reader.close(); count = integer.parseint(strcount); } catch (exception e) { count = -1 } return count; }
ok, have bit of context, here actual question. if chance fail fetch count.txt file , cannot tell how many images there in remote folder need tell user have failed using dialog , ask them if want retry or ignore it. how go doing that? next bit of code attempt fails horribly couple of reasons, because dialogs shown synchronously. how implement (retry/cancel) galleryadapter state network , can server images in gallery?
here's my, non working attempt. alternatives suggestions welcome.
public int getcount() { // todo fetch image amount remotely int imagecount; { remotebitmaphelper rbmh = new remotebitmaphelper(); imagecount = rbmh.getbitmapcount(baseurl + "count.txt"); if(imagecount == -1) { imagecount = 0; new alertdialog.builder(this.context) .settitle("connection error") .setmessage("there error connecting server") .setpositivebutton("retry", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int whichbutton) { setretrygetcount(true); } }) .setnegativebutton("continue", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { setretrygetcount(false); } }) .setcancelable(false) .show(); } } while(retrygetcount && imagecount <= 0); return imagecount; }
this not perfect idea. first try image count , try retrieve images. actually, should try this example. although fetches images external storage, later can convert fetch images server. hope you.
Comments
Post a Comment