Picasso library: Loading Image from URL (HTTP)

ImageView is used to display an image, such as an icon. It can load images from various sources such as resources folders or content providers. But, how to load image from URL? Just by adding URL to ImageView won’t load directly. First we need to store the image in temporary location then we can attach to Imageview. So, we should load images from the Web in an application, their processing and displaying with the limited memory. Images come in all shapes and sizes. In many cases they are larger than required for a typical application user interface. We should decode large bitmaps without exceeding the per application memory. It is necessary to ensure working with the memory to prevent OutOfMemoryError . It is a time consuming process to specific needs. So what is the best and easiest way to do this..To solve all the pain, Jake Wharton(Author of ActionBarSherlock) has actually created an open source library named "Picasso" for image downloading and caching on Android-often in only one line of code!

Download Picasso library

Steps loading image from URL:

1. Create a new project in Eclipse and add Picasso library to the pjoject. (help to add library)

2. Inorder to load images from a url we first need to add required permission to
AndroidManifest.xml file.

To access internet required permission is – INTERNET

AndroidManifest.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bcliks.imagefromurl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <!-- Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    

    <application        
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         >
        
        <activity android:name="com.bcliks.imagefromurl.LoadImageFromURLomURL"
            android:label="@string/app_name"> 
            <intent-filter>             
                     <action android:name="android.intent.action.MAIN" />
                  <category android:name="android.intent.category.LAUNCHER" />                
         </intent-filter>            
     </activity>
 
    </application>
</manifest>

3. For testing purpose create a simple ImageView in your main.xml file

main.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
 
    <ImageView android:id="@+id/image"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_margin="10dip"/>
 
</LinearLayout>

4. Open your main activity and type the following code. Whenever you want to show an image from url just call the following code.
1
Picasso.with(getApplicationContext()).load(image_url).into(imgView);

LoadImageFromURLomURL.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.bcliks.imagefromurl;
 
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.ImageView;
 
public class LoadImageFromURLomURL extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Imageview to show
        ImageView imgView = (ImageView) findViewById(R.id.image);
         
        // Image url
        String image_url ="http://www.empireonline.com/images/uploaded/wreck-it-ralph-box-office.jpg";
         
        // whenever you want to load an image from url
        // call below function
        // url - image url to load
        // placeholder- loader image, will be displayed before getting image
        // image - ImageView     
   
               Picasso.with(con).load(image_url) 
           .placeholder(R.drawable.icon_placehoder)
           .error(R.drawable.icon_img_error)
           .resizeDimen(80, 80)         
           .centerInside()            
           .into(imgView);    
  }
}




 Result:

Useful links:

Top 10 Android websites




Android, the little green robot is an open source operating system for a wide range of mobile devices and tablets. In this blog, I post some very useful android stuffs for beginners. This blog assumes that you have some hands-on experience with the basics of Java such as Classes, Objects, Methods etc.

Here are the 10 Most Popular Android Sites you don’t want to miss in my collections, which covers latest Android news, articles and tutorials.

1. Android Developers
The official Android developer website, always get the latest Android official documentation here.

2. Android Developers Blog
The official Android developer blog, always get the latest Android news here.

3. Mkyong 
This is a best site to start Android programming from basics,here you can find excellent basic tutorials to get you start program Android easily.

4. Vogella 
Well-known Android website, you just can’t miss this one. It covers from Android Installation to all Core Concepts. 

5. AndroidHive 
Many Android programming tutorials and source code examples, well organized by categories.I am glad to say this Android site from India.

6.Stack Overflow
Well-known generic programming Q & A site. Android topics included.

7.Styling Android
Best Android UI website. Covers styling and, in particular, theming for Android apps.

8.The Code of a ninja 
Collection of Android tips, tools & tutorials.

9.Mobiletuts+ 
It's all about quality tutorials for mobile developers.

10.Android Central 
Android news and articles

If you have other great Android websites, please leave a comment to share with us.