开发者

Loading Images from Web Directory, display in ImageView

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();
        }
    }

}


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜