imin
imin

Reputation: 4578

populate gallery with images from web

the tutorial here http://www.edumobile.org/android/android-beginner-tutorials/creating-image-gallery/ teaches how to put images from resources in gallery

here's the part where it set the images to be displayed:

private Integer[] mImageIds = {
            R.drawable.icon,
            R.drawable.icon,
            R.drawable.icon
    };

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);
        return i;
    }

but i want to display a couple of images from the web instead. so here's what I did:

String gallery1 = "http://www.myimages.com/1.png";
URL ulrn = new URL(gallery1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);

    if (null != bmp){
      bmp = getResizedBitmap(bmp,150,120);  //resize the thumb
      i.setImageBitmap(bmp);
      i.setLayoutParams(new Gallery.LayoutParams(150, 100));
      i.setScaleType(ImageView.ScaleType.FIT_XY);
      i.setBackgroundResource(mGalleryItemBackground);

but using the method I did above I could only display 1 picture inside the gallery, where as i need to also display another pictures inside the gallery, for example from http://www.myimages.com/2.png and http://www.myimages.com/3.png. How can I do this?

Upvotes: 1

Views: 1278

Answers (2)

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

Here you call Image link one time but Gallery baseAdapter class getView Call for Every item. you need to call for every time.

Create a Array For Url. Like

String[] URl={http://www.myimages.com/2.png , http://www.myimages.com/,png,http://www.myimages.com/2.png};

and Call this Method From Your getView Method

   public Drawable ImageOperations(String imageurl) {
        try {
            InputStream is = (InputStream) new URL(imageurl).getContent();
            Drawable drawable = Drawable.createFromStream(is, "src");
            return drawable;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }

    }

and Then Edit some Line of your getView Method.

i.setImageDrawable(ImageOperations(URl[position]));

Thanks

Upvotes: 1

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Use Below Link Code of Lazy Loading Listview, Change Listview to Gridview as per your requirement, it may help you.

Lazy Loading Listview

Upvotes: 0

Related Questions