Android Picasso Library Load Single image from URL example tutorial Android Studio

How to create android application using Picasso image loading library.

Picasso image downloading and caching library is one of the best image loading libraries available on GitHub. This library gives us the facility to use its own created classes code into our android applications and all we just have to do is pass the argument with pre define functions. So here is the complete step by step tutorial for Android Picasso Library Load Single image from URL example tutorial Android Studio.
android-project-download-code-button

Benefits of using Picasso Library :

  • Small line of code to load image from internet.
  • Pre place holder image functionality.
  • Network error or Image not found error image functionality.

What is Place holder image in Picasso library ?

Place holder image is the pre fixed image into ImageView while Picasso library is loading image form internet and when image finish downloading and set into imageview then the place holder image will be automatically hidden.

What is the Error image in Picasso library ?

Error image is one of the most important features of Picasso because of this functionality if in some case the net connection is not available on android device or the image has been deleted from server then by default after loading procedure it will automatically set the error image into image view so application user will understand that the network connection or image is not showing properly.

Note: Read below steps very carefully to add Picasso  GitHub library inside your current project.

1. Open your project’s build.gradle ( Module : app ) file.

build-gradle

2. Please add below code inside your build.gradle ( Module : app ) file.

compile 'com.android.support:appcompat-v7:24.0.0'

compile 'com.squareup.picasso:picasso:2.5.2'

3. Screenshot of build.gradle ( Module : app ) file after adding above code.

picasso-library

4. Please add internet permissions inside your AndroidManifest.xml file .

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Next step is start coding.

Android Picasso Library Load Single image from URL example tutorial Android Studio.

Code for MainActivity.java file.

package com.android_examples.picassolibrary_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    ImageView imageview;
    Button button ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);
        imageview = (ImageView)findViewById(R.id.imageView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ShowImageUsingPicasso();

            }
        });
    }

    public void ShowImageUsingPicasso(){

        Picasso.with(MainActivity.this)
                .load("https://www.android-examples.com/wp-content/uploads/2016/03/demo_image.jpg")
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .resize(300,300)
                .into(imageview);

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.picassolibrary_android_examplescom.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to load image from HTTP URL using Picasso image library"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="154dp" />


</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.picassolibrary_android_examplescom">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    s
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshot :

Android Picasso Library Load Single image from URL example tutorial Android Studio

Click here to download Android Picasso Library Load Single image from URL example tutorial Android Studio project with source code.

8 Comments

  1. hello sir first of all thanks for sharing your valuable knowledge over internet so all can learn from it and second thing is that i can not store my image in my database i know how to store text files and get them from server but i don not know about the images so could you please tell me the way to do so i want to store my image and it should be accessible through url so please tell me how to do so like your url links to an image thanks

Leave a Reply

Your email address will not be published. Required fields are marked *