0

I am trying to write a simple application that loads images that are stored in a web directory I've created. I want the application to grab ALL of the images that are stored in said directory and display them in a layout.

The code below works for only certain type of URLs(mainly tinypic, but not photobucket, imageshack etc) and also not for directories.

I've my image names formatted in sequence like image1.png, image2.png, etc. so that the code snippet and the downloadFile()-method would work.

But, I'm not sure how I would grab all the images from the server simultaneously because this only grabs one image at a time, sets it as a bitmap and then displays it in an IV. I'm pretty new to Android so some help in the right direction would be appreciated.

public class NewWallpapersActivity extends Activity {

    ImageView imView1;
    String imageUrl="http://i133.photobucket.com/albums/q44/slimjady/Wallpapers";
    Random r= new Random();

    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.newwallpapers);

        Button bt3= (Button)findViewById(R.id.get_imagebt);
        bt3.setOnClickListener(getImgListener);
        imView1 = (ImageView)findViewById(R.id.IV1);
    }    

    View.OnClickListener getImgListener = new View.OnClickListener(){

        public void onClick(View view) {
            //i tried to randomize the file download, in my server i put 4 files with name like
            //png0.png, png1.png, png2.png so different file is downloaded in button press
            int i =r.nextInt(4);
            downloadFile(imageUrl+"png"+i+".png");
            Log.i("im url",imageUrl+"png"+i+".png");
        }

    };

    Bitmap bmImg;

    void downloadFile(String fileUrl){
        URL myFileUrl = null;          
        try {
            myFileUrl= new URL(fileUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();

            bmImg = BitmapFactory.decodeStream(is);
            imView1.setImageBitmap(bmImg);
        } catch (IOException e) {
            String exception = "Erro: An Exception Was Thrown";
            Toast errortoast = Toast.makeText(this, exception, Toast.LENGTH_SHORT);
            errortoast.show();
            e.printStackTrace();
        }
    }

}

1 Answer 1

2

Here are some thoughts on this one:

Displaying all Images

Since you want to display all Images from within a single folder, I would suggest you to have a Directory-Activity, which displayes all the images in the given directory and one Image-Activity which displays only one single Image.

To proper present this I would suggest using a GridView to display all the Images and a normal Activity with a full-screen ImageView to display the single Image.

You could also use the Gallery-widget to "combine" both those ideas.

Not letting the user wait

One of the bigger problems with your approach is, that the images are getting downloaded on the UI-Thread.

Depending on the size of all or on single image and the available connection-speed, Downloading the image can take a while. While the images are getting downloaded, the UI-Thread will wait for your downloadStuff()-method to return and the UI will freeze. This might create the illusion that your App just crashed.

So, you'll want to do the downloading in a separate thread. Android has a handy wrapper for this called an AsyncTask.

As a little bonus to show your user that the process might take a while you can use a ProgressDialog to illustrate this (on the UI-Thread).

Knowing whats in a directory

Now we come to the point where we have to face some limitations. If you want to use Picture-Hosters like the ones you listed above, you'll need to check if they offer you an API (or something equal) to get a list of Images (or their URLs) from a certain directory/album (look for a "API"-link on the front-page).

If you host the images on your own Server, you should be able to create a little PHP-script (or whatever script/programming language you prefer), which then lists all image-files in the directory and presents them in an easy-to-parse way (either JSON or XML).

This should enable you to get a list of URLs to the Image-files you want to display.

2
  • Thank you so much for the in depth response brother. But a quick question on the directory issue. I see why I would have to create a directory activity that would use the API to get all of the files/urls, but would I then call this Activity using the AsyncTask option you mentioned? I will b sure to go through all the links you suggested, that's a sea of resources there! Thanks again, man Oct 6, 2011 at 20:28
  • No, you would actually start the downloading of the Images in an AsyncTask when the Activity is opened (in the onStart()-method for example). You can update your UI from an AsyncTask, an example might be found here Oct 6, 2011 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.