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 8904027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:01:09+00:00 2026-06-15T02:01:09+00:00

I am trying to display image from internet in a Gridview. When I am

  • 0

I am trying to display image from internet in a Gridview. When I am running my program there is not error in the catlog but no image are displayed in my screen..

ImageAdapter.java :

public class ImageAdapter extends BaseAdapter {
private Context mContext;
Bitmap bmImg;
ImageView imageView;

public String[] mThumbIds = {
        "http://i724.photobucket.com/albums/ww246/bnhenson/squishy.jpg", "http://cdn4.teen.com/wp-content/uploads/2012/07/paranorman-zayn-malik-tweets.jpg",
        "http://xc0.xanga.com/08fe2b7a30c37281496919/m224298958.jpg"
};

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
    imageView = new ImageView(mContext);
    downloadFile(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
    imageView.setPadding(0, 0, 1, 0);
    return imageView;
}
void downloadFile(String fileUrl) {

    AsyncTask<String, Object, String> task = new AsyncTask<String, Object, String>() {

        @Override
        protected String doInBackground(String... params) {
            URL myFileUrl = null;
            try {
                myFileUrl = new URL(params[0]);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                HttpURLConnection conn = (HttpURLConnection) myFileUrl
                        .openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();

                bmImg = BitmapFactory.decodeStream(is);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }
        protected void onPostExecute(String unused) {
            imageView.setImageBitmap(bmImg);
        }
    };
    task.execute(fileUrl);

}}

AndroidGridLayoutActivity.java

public class AndroidGridLayoutActivity extends Activity {

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

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));
}}

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >  

</GridView>

And in my manifest I added this two line :

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

Does anyone have an idea, what step I miss here?

Thanks in advance for your help,

Germain.

  • 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-15T02:01:10+00:00Added an answer on June 15, 2026 at 2:01 am

    You should create a separate ImageView object for every cell of your grid, not use a global variable. To do so, so something like that:

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext); 
        downloadFile(imageView, mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
        imageView.setPadding(0, 0, 1, 0);
        return imageView;
    }
    
    void downloadFile(ImageView imageView, String fileUrl) {
        // ...
        // change your AsyncTask so you can pass imageView as well
        // instead of setting loaded bitmap to global mImageView, set it to the passed in imageView.
        // ...
    }
    

    To change your AsyncTask, either change the type for params to Object so you can pass imageView and fileUrl to doInBackground() or create a class for your AyncTask with a constructor you can pass both objects and store them in variables local to the AsyncTask so you can access them from the doInBackground() method.

    Also note that, if you have more items in your grid, you should reuse the passed in convertView when it has a non-null value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display an image returned from AFImageRequestOperation ImageRequestOperationWithRequest: but it appears that
I am using this code to display image from URL, but it is not
i am trying to display image from internet in my imageview. which is raising
I have been trying to display image from database using php but i am
In my program I am trying to display an image from a file using
I'm trying to display image data read in from a binary file (I have
I am trying to display an image in a gridview, however, the columns that
friends, i am trying to display image from gallery now i want to put
i am trying to display an image from net..and i am using ignition library
i'm trying to display an image from the database in an ASP.NET web page.

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.