2

I have a URL where some images are exists. I want to retrieve all the images from that URL and display these images in the GridView. Now when I click any of thumb preview like in grid view then it should enlarge or load to full screen.

putting the snap shots for better understandingenter image description here

anticafe
  • 6,816
  • 9
  • 43
  • 74
DEVANG SHARMA
  • 2,662
  • 5
  • 33
  • 52
  • 1
    What exactly do you need help with? Making the gridview? Loading the images? Preventing locking of the UI when loading the images? Displaying the detail image? – Caspar Harmer Jun 13 '11 at 04:26

2 Answers2

6

You can try the following code. Lazy loading the image is one good solution to load the images, you can try the lazy loading from the following link: Lazy load of images in ListView . In this they have used a ListView in the layout so the images and the corresponding text are shown as list items, you can change that ListView to a GridView something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView
        android:id="@+id/gridv"
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:columnWidth="90dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Clear Cache"/>
</LinearLayout>

I think this will solve the issue.

Community
  • 1
  • 1
Basil
  • 2,163
  • 2
  • 16
  • 25
5

First figure out a way of how you are going to download all the images from a single link, which I believe is somewhat difficult.

Then put all the link locations into a string array. now use the below code to download the images.

    public Drawable LoadImage(String url) {

    Drawable d;
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (NullPointerException e) {
        d = getResources().getDrawable(R.drawable.icon);
        return d;
    } catch (Exception e) {
        d = getResources().getDrawable(R.drawable.icon);
        return d;
    }
}

Get the length of the string array in which you have stored the link locations. And inside of a for loop try executing the above code. This will return an drawable object which you can convert into either resources or Bitmap and add it to the GridView.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • You should use a background thread for the connection. Otherwise your program will block and show an "application not responding" dialog after about 5 seconds. –  Jun 13 '11 at 06:11
  • This solution will fail if Android SDK version < 2.2, before 2.2, it should use httpconnection. – Cheung Jul 12 '12 at 15:53