kostas
kostas

Reputation: 53

image gallery with images from url

i would like to create an image gallery that get pictures from a url..this is my code but is not working...

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;


public class gallery extends Activity {
    private Gallery gallery;

    private ImageView imgView;


    private String[] myRemoteImages = {

            "http://www.kostas-menu.gr/chania/venizelou.jpg", 
            "http://www.kostas-menu.gr/chania/faros.jpg",
            "http://www.kostas-menu.gr/chania/giali.jpg",
            "http://www.kostas-menu.gr/chania/kipos.jpg"

    };


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.gallery);


        imgView = (ImageView)findViewById(R.id.ImageView01);

        imgView.setImageResource(myRemoteImages[0]);


         gallery = (Gallery) findViewById(R.id.examplegallery);

         gallery.setAdapter(new AddImgAdp(this));


         gallery.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView parent, View v, int position, long id) {

                imgView.setImageResource(myRemoteImages[position]);

            }

        });


    }


    public class AddImgAdp extends BaseAdapter {

        int GalItemBg;

        private Context cont;


        public AddImgAdp(Context c) {

            cont = c;

            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);

            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);

            typArray.recycle();

        }


        public int getCount() {

            return myRemoteImages.length;

        }


        public Object getItem(int position) {

            return position;

        }


        public long getItemId(int position) {

            return position;

        }


        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);
            try {
                                /* Open a new URL and get the InputStream to load data from it. */
                                URL aURL = new URL(myRemoteImages[position]);
                                URLConnection conn = aURL.openConnection();
                                conn.connect();
                                InputStream is = conn.getInputStream();
                                /* Buffered is always good for a performance plus. */
                                BufferedInputStream bis = new BufferedInputStream(is);
                                /* Decode url-data to a bitmap. */
                                Bitmap bm = BitmapFactory.decodeStream(bis);
                                bis.close();
                                is.close();
                                /* Apply the Bitmap to the ImageView that will be returned. */
                                i.setImageBitmap(bm);
                        } catch (IOException e) {
                                i.setImageResource(R.drawable.error);
                                Log.e("DEBUGTAG", "Remtoe Image Exception", e);
                        }

    }


}

the error is that i cant use imgView.setImageResource(myRemoteImages[0]); for strings...ant hepl please?

Upvotes: 3

Views: 9454

Answers (1)

Paresh Mayani
Paresh Mayani

Reputation: 128428

I suggest you to use this LazyLoader for the images fetching from URL Lazy load of images in ListView.

For your case, i mean to load images from web inside the gallery view, use the same adapter which have been given in the example link.

Upvotes: 3

Related Questions