kirktoon1882
kirktoon1882

Reputation: 1231

Android ViewPager: downloading the images from URL

I'm trying to create a image Gallery using ViewPager. I've found several examples and I've got them all to work. The one thing the examples don't show, is how to populate the ViewPager using downloaded images from an URL. All the tutorials either have the images in the Drawables folder or on the SD card. Does anyone know of an example where a ViewPager gets the images from a URL. I've found examples of Lazy Loading a GridView and a ListView, but I'm not smart enough to be able to convert those into a working ViewPager. I mainly run into issues with the Adapter classes. The Lazy Loading examples seem to use BaseAdapter and I think a ViewPager needs to use PagerAdapter right? So, are there any examples of a ViewPager that gets the images from a URL? What I need the ViewPager to do is to download the images from a String Array something like this:

    String[] imageGalleryStr = {
    "http://www.website.com/images/image01.png",
    "http://www.website.com/images/image02.png",
    "http://www.website.com/images/image03.png",
    "http://www.website.com/images/image04.png",
    "http://www.website.com/images/image25.png",
    "http://www.website.com/images/image26.png",
    "http://www.website.com/images/image27.png",
    "http://www.website.com/images/image28.png" };

Any ideas?

Upvotes: 3

Views: 9590

Answers (4)

Robert Rowntree
Robert Rowntree

Reputation: 6289

If you already have a working template (ViewPager) maybe you could choose one of the libs for getting images/bitmaps from across the net and then wrap a loader for images inside the Fragment type being operated on by the ViewPager...

When the pager calls 'onCreateView' on the type being paged, you have something like:

imgLoadr.DisplayImage(mMap.get("pic"), imgView);

Your implementation of 'DisplayImage' is built on one of the many libs for networked image management and includes the following details:

DisplayImage implementation looks first for a properly scaled bitmap in memory...
then it looks for a bitmap in local file system cache...
then it fetches the file from across the network and feeds the network stream to get instances of local bitmap/caches.

Your post only concerned the last item above, but IMO you may have to become interest in all of the list because that is just part of working with an image-centric app..

If you are going to be dealing with large album of photos, you probably will have to look at efficiently managing each bitmap and scaling the bitmaps in some way to cut your requirements for memory.

Upvotes: 0

Ankit Popli
Ankit Popli

Reputation: 2837

I haven't tried this code but hopefully something like this should work for you. :)

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    tagShown = false;

    View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.image_view_item, container, false);

    imageView = (ImageView) view.findViewById(R.id.imageView);

    new DownloadImageInBackground(imageView).execute(imageGalleryStr[position]);

    ((ViewPager) container).addView(view);

    return view;
}

...

private class DownloadImageInBackground extends AsyncTask<String, Void, Bitmap>{

    ImageView iv;

    public DownloadImageInBackground(ImageView iv) {
        this.iv = iv;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        // download bitmap from string url
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        iv.setImageBitmap(result);
    }
}

Upvotes: 2

Diego Palomar
Diego Palomar

Reputation: 7061

I would recommend you use the Volley library, the same library used by Google in its application Google Play. Please check this answer: Getting Bitmap images from server to set on ImageView

Upvotes: 1

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

use this library for image galary https://github.com/nostra13/Android-Universal-Image-Loader

Upvotes: 4

Related Questions