Reputation: 47
I want to load heaps of images from a URL into my gallery. At the moment they are stored in the drawable directory but i want to load them from a URL link like http://i200.photobucket.com/albums/aa216/example/color.jpg instead of @drawable/image1
Here is the code i have.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<TextView
style="@style/ImageTitle"
android:text="@string/image1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/image2" />
<TextView
style="@style/ImageTitle"
android:text="@string/image2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/image3" />
<TextView
style="@style/ImageTitle"
android:text="@string/image3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/image4" />
<TextView
style="@style/ImageTitle"
android:text="@string/image4" />
</RelativeLayout>
</ViewFlipper>
<LinearLayout
style="@style/ButtonContainer"
android:orientation="horizontal" >
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/media_play" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/media_pause" />
</LinearLayout>
<ImageView
android:id="@+id/swipe_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/arrowleft" />
<ImageView
android:id="@+id/swipe_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrowright" />
</RelativeLayout>
Upvotes: 1
Views: 3903
Reputation: 779
Uae UniversalImageLoader Library from Github.
Download the project as zip & import the library in your project. Then from your java class,
ImageView imageView = (ImageView) findViewById(R.id.your_view)
String imageUrl = "your_url";
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(imageUrl, imageView);
Upvotes: 1
Reputation: 12526
You can use Universal image loader
library
http://github.com/nostra13/Android-Universal-Image-Loader
ImageView imageView = ... // view, where the image will be displayed
String imageUrl = ... // image URL
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(imageUrl, imageView);
It is very easy to use and takes care of most of the things like caching etc
If you do not want to use any external libraries, You can write your own code to download and display image too.
Follow this tutorial
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
Upvotes: 0