RSS

Android Tutorials – Load image from URL to an ImageView

28 Jan

Loading an image that isn’t defined in your resource folder in android can be really tricky. The image has to be downloaded asynchronously, cached in the phone in case we want to reuse later and then display it in the ImageView. Luckily there are many libraries that do just that and better. I am going to be using the ImageLoader lib.

    1. Download the ImageLoader lib here. (Google ‘android ImageLoader’ if link fails)
    2. Place the jar file in the libs folder in your project.
    3. This piece of code should do the trick:
String image_url = "https://mycodeandlife.files.wordpress.com/2013/01/384088_2317070728022_2086719259_n.jpg";
ImageView image = (ImageView) findViewById(R.id.imageView1);
                        // Get singletone instance of ImageLoader
			ImageLoader imageLoader = ImageLoader.getInstance();
			// Initialize ImageLoader with configuration. Do it once.
			imageLoader.init(ImageLoaderConfiguration.createDefault(this));
			// Load and display image asynchronously

			DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .showStubImage(R.drawable.ic_launcher) //this is the image that will be displayed if download fails
			.cacheInMemory()
			.cacheOnDisc()
			.build();

			imageLoader.displayImage(image_url, image, options);

This is the image. 384088_2317070728022_2086719259_n

The last bit is to add permissions to the Android Manifest. Add the following permissions: android.permission.WRITE_EXTERNAL_STORAGE and android.permission.INTERNET.

That’s it. Of course there still a lot more. You can use this in a list view and even show progress. You can read more here

Advertisements
 
8 Comments

Posted by on January 28, 2013 in Uncategorized

 

8 responses to “Android Tutorials – Load image from URL to an ImageView

  1. Fany Kirui

    April 8, 2013 at 11:33 am

    great stuff Gilbert. Keep it up.

     
  2. Need some help here

    June 27, 2013 at 6:24 am

    I tried this, but kept getting these errors:
    ImageLoader cannot be resolved into a type.
    ImageLoaderConfiguration cannot be resolved into a type.
    DisplayImageOptions cannot be resolved into a type.
    It asks me to make the three classes.

    HELP!

     
    • Need some help here

      June 27, 2013 at 6:25 am

      Even though I have placed the library in the libs folder

       
      • Gilbert Kimutai

        June 27, 2013 at 2:46 pm

        add the following permissions:

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

         
  3. Anil Sharma

    January 28, 2014 at 12:20 pm

    Yes i have same problem also. Gilbert plz give the solution.

     
  4. Anil Sharma

    January 29, 2014 at 5:48 am

    hmmm thanx gilbert its working.

     
  5. Anil Sharma

    January 29, 2014 at 1:30 pm

    Gilbert i have an arraylist of image urls. then i can i set this arraylist on a gridview. i hope u will solve my problem. plzz help me.
    thanx in advance.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: