Downloads images from URL and display it in a GridView
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 understanding
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.
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.
精彩评论