user1197941
user1197941

Reputation: 179

Loading images from web into image gallery

I am using the following to code make an image gallery, but I would like to use images from the web instead of from the SD card. Is there a way to do this? Currently the code requires and integer value for the image. Or do I have to download the images to the SD first?

    public class viewimages extends Activity {

private Gallery gallery;
private ImageView imgView;

private Integer[] Imgid = {
        R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewimages);

    imgView = (ImageView)findViewById(R.id.ImageView01);    
    imgView.setImageResource(Imgid[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(Imgid[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 Imgid.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 imgView = new ImageView(cont);

        imgView.setImageResource(Imgid[position]);
        imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

   }

Upvotes: 0

Views: 1653

Answers (1)

RyanInBinary
RyanInBinary

Reputation: 1547

You can do something like this:

URL url = new URL("http://www.website.com/image.jpg");
Bimtap bmp = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());

Then you'll need to change where you're doing "setImageResource" to "setImageBitmap" and supply the 'bmp' object once it has been downloaded and loaded up.

[Edit]

It's also important to make sure your android manifest has internet permissions.

<uses-permission android:name="android.permission.INTERNET" />

[Edit for comment question]

If you want to do this for multiple images, and the images aren't based on an id_number (basically if the images aren't something like 0001.jpg, 0002.jpg, etc.) create a string ArrayList that will hold the names of all the items, and then iterate through it. E.g.,

List<String> imageNames = new ArrayList<String>();
imageNames.add("image.jpg");
imageNames.add("thisPhoto.jpg");
//etc. until you've added all images

for (int i = 0; i < imageNames.size(); i++){
    URL url = new URL("http://www.website.com/" + imageNames.get(i));
    Bimtap bmp = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());
}

Hope that helps

Upvotes: 2

Related Questions