开发者

Implementing a GalleryAdapter getCount that fetches its content from a remote server

This is a looong explanation, but please bear with me... Here be dragons...

Ok so I have an Android Gallery and implemented a GalleryAdapter that creates my views and stuff:

public class GalleryAdapter extends BaseAdapter {....
....
etc
...

Now, before we start, almost ALL of the examples out there that show you how to do this assume that you know exactly how many images there are going to be in the gallery. In my case, I do not. It could be any arbitrary number of images but at least there's a consistent naming convention like 001, 002, 003 etc

I have a folder somewhere on a server that has the images in it that should be displayed in the gallery:

http://www.somedomain.com/content/pictures

In that pictures folder we might have

0001.jpg

0002.jpg

0003.jpg

000n.jpg

etc ad infinitum

now BaseAdapter which I am extending, expects you to override public int getCount()

So if you know that you have 20 images you simply go:

public int getCount() {return 20;}  

And your on your merry way. But what about my case?

The way I have gotten around this till now is requiring a count.txt file be placed in the root of that directory where the images are that contains an integer that tells you that there are X amount of images in it. Remember, this is an http location.

So my getCount looks more like this

public int getCount() {
  int count = getRemoteImageCount();
  if(count == -1)
  {
      return 0;//Oh crap, something went wrong, what now?!
  } else {
    return count;
  }

public int getRemoteImageCount()
  try {
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();
    conn.setUseCaches(true);
    conn.connect();

    InputStream inst开发者_高级运维ream = 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, now that we have a bit of context, here is the actual question. If by some chance I fail to fetch the count.txt file and cannot tell how many images there are in the remote folder I need to tell the user that we have failed using a Dialog and ask them if they want to retry or just ignore it. How would I go about doing that? The next bit of code is my attempt but it fails horribly for a couple of reasons, but mainly because Dialogs are shown synchronously. How do I implement a (Retry/Cancel) to get my GalleryAdapter into a state where eventually the network is back up and I can server those images in the Gallery?

Here's my, non working attempt. Any alternatives suggestions are also welcome.

public int getCount() {
    // TODO fetch image amount remotely     
    int imageCount;

    do {
        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 was an error connecting to the 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 is not a perfect idea. First you try to get image count and then try to retrieve images. Actually, you should try this example. Although it fetches images from external storage, but later you can convert it to fetch images from server. I hope this will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜