How to load image from url android Example

 

Hi, Developer in this Android tutorial I am Sharing How to load image from URL android Example. here you can read an easy way to load an image on imageView From URL in android API Bitmap Image om ImageView with Picasso Library. in this article solve your android development like show image in Profile Activity, Show Image in Android ListView,  Display Image from URL in CircularImageView

  1. Add an CircularImageView in the XML layout.
  2. Get the instance of  ImageView in Activity class.
  3. Add Picasso library in your project Gradle module
  4. Display or show image on CircularImageView

 

Step 1:- Add a CirculerImageView Layout in your XML.

activity_main.XML

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:paddingRight="5dp"
    android:orientation="vertical">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/Profile"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="right"
        android:layout_marginTop="50dp"
        android:src="@drawable/icon_user"
        android:tint="@color/colorAccent"
        app:civ_border_color="@color/colorAccent"
        app:civ_border_width="2dp" />

AndroidManifest.xml

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

build.gradle

compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.android.support:cardview-v7:27.+'
compile 'com.android.support:recyclerview-v7:27.+'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.volley:volley:1.0.0'

Step 2: Main Activity java class Add These code.

in java class get the UI instance and find id and implement this. and load a Url From JSON API and Show Display Image on CircularImageView from Image API Url.


public class Profile extends AppCompatActivity {

    ImageView ProfileImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        ProfileImage=(ImageView)findViewById(R.id.Profile);
       Picasso.with(Profile.this)
       .load(Image)
       .placeholder(R.drawable.icon_user)
       .into(ProfileImage);
}

Picasso is a library in android to load an image from URL 

Android Picasso features :

  • Resizing and Scaling
  • Center Cropping
  • Rotation and Transformation
  • Setting the Placeholder and Error images
  • Fading
  • Disk and Memory Caching
  • Priority Requests
  • Support for Request cancellation and parallel downloading

Android Picasso – Loading image from URL

  1. Drawable: Android  Loading image from drawable image into an ImageView
  2. URL: To load an image from  URL, the URL is enclosed as a String inside the load() method
  3. Error: An error drawable is generally used in cases where there’s a failure in loading the image.  inside .error() method.
  4. Callback: Picasso provides callback methods for check of the status of the image loaded (success/error).
    
    Picasso.with(this).load("www.codeplayon.com").error(R.mipmap.ic_launcher).into(imageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            Log.d("TAG", "onSuccess");
                        }
    
                        @Override
                        public void onError() {
                            Toast.makeText(getApplicationContext(), "An error occurred", Toast.LENGTH_SHORT).show();
                        }
                    });
    
  5. Resize: Picasso allows  to  resize the image before displaying  an ImageView by invoking  resize() method and passing the width and height in pixels
  6. Rotate: To rotate an image pass the float value inside the rotate() method.
  7. Scale: Resizing an image can cause stretched images. To keep the aspect ratio intact use centerCrop() or centerInside() with the resize() method.
    fit() is like a delayed resize(), that reduces the image to fit the ImageView bounds.
  8.  centerCrop and centerInside can’t be used without calling resize() with a positive width and height
  9. Targets:  Targets are an interface that would return the bitmap image along with its placeholder and error drawable(if defined). We can further customize the bitmap image returned in the method onBitmapLoaded() or directly show it in the ImageView

 

You may also like...