Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:59:46+00:00 2026-06-09T14:59:46+00:00

I would like to create an image gallery that get pictures from a url..this

  • 0

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

 public class ImagesActivity extends Activity
 {    

private String[] imageIDs= {"http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",  
"http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",  
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",  
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg"
};

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

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

    gallery.setAdapter(new ImageAdapter(this));        
    gallery.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, View v, int position, long id) 
        {                
 //                             Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", Toast.LENGTH_SHORT).show();
                          //---display the images selected---
                            ImageView imageView = (ImageView) findViewById(R.id.image1);                
                            imageView.setImageResource(imageIDs[position]);
        }
    });

}


public class ImageAdapter extends BaseAdapter 
{
    private Context context;
    private int itemBackground;

    public ImageAdapter(Context c) 
    {
        context = c;
        //---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(
            R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();                    
    }

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }            

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}    

}

ViewsActivity.java

 public class ViewsActivity extends Activity 
 {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 //        setContentView(R.layout.main);

    startActivity(new Intent(this, ImagesActivity.class));

  }
 }

The error is that i cant use imageView.setImageResource(imageIDs[position]); for strings…ant hepl please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:59:48+00:00Added an answer on June 9, 2026 at 2:59 pm

    You need to decode the image from the link for example,

    URL url = new URL("load your URL");
    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    img_downloaded.setImageBitmap(bmp);
    

    Update:

    I modified your code, you may try with this,

    public class MainActivity extends Activity {
    
        private String[] imageIDs = {
                "http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",
                "http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",
                "http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",
                "http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg" };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position,
                        long id) {
                    // Toast.makeText(getBaseContext(), "pic" + (position + 1) +
                    // " selected", Toast.LENGTH_SHORT).show();
                    // ---display the images selected---
                    ImageView imageView = (ImageView) findViewById(R.id.imageview1);
                    URL url = null;
                    try {
                        url = new URL(imageIDs[position]);
                        Bitmap bmp = null;
                        try {
                            bmp = BitmapFactory.decodeStream(url
                                    .openConnection().getInputStream());
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        imageView.setImageBitmap(bmp);
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                }
            });
    
        }
    
        public class ImageAdapter extends BaseAdapter {
            private Context context;
            private int itemBackground;
    
            public ImageAdapter(Context c) {
                context = c;
                // ---setting the style---
                 TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
                itemBackground = a.getResourceId(
                 R.styleable.Gallery1_android_galleryItemBackground, 0);
                 a.recycle();
            }
    
            // ---returns the number of images---
            public int getCount() {
                return imageIDs.length;
            }
    
            // ---returns the ID of an item---
            public Object getItem(int position) {
                return position;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            // ---returns an ImageView view---
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView = new ImageView(context);
                // imageView.setImageResource(imageIDs[position]);
                URL url;
                try {
                    url = new URL(imageIDs[position]);
                    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection()
                            .getInputStream());
                    imageView.setImageBitmap(bmp);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
                    imageView.setBackgroundResource(itemBackground);
    
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return imageView;
            }
        }
    
    }
    

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <Gallery
            android:id="@+id/gallery1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/gallery1" />
    
    
    </RelativeLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a single URL that returns one image when loaded
I would like to create from this image a styled DIV box for my
I would like to create a gallery of images but when one image is
I would like to create a button that when clicked a static image is
I'm looking to create something like in this image. Each block would have an
I would like to create a very simple image gallery. I am trying to
I'd like to create a full screen android gallery. In this gallery I would
I would like to create this shape using just css. I am pretty sure
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a class that runs something (a runnable) at regular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.