how to load an imageview from URL in android

How to load an ImageView from URL in Android


The best solution to do it we prefer to use the library, because if it it can keep the image in cache and will not be reloading image every time it request.

Introduction

Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!


Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

Many common pitfalls of image loading on Android are handled automatically by Picasso:

Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.


Setup Permission

Adding Internet Permission to our Manifest.xml file:

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

It will allow your app to use internet for downloading images.

Setup Picasso

Adding Picasso to our app/build.gradle file:

dependencies 
    implementation 'com.squareup.picasso:picasso:2.5.2'
}


Loading an Image from Url

We can then load a remote image into any ImageView with:

String imageUri = "https://i.imgur.com/tGbaZCY.jpg";
ImageView ivBasicImage = (ImageView) findViewById(R.id.ivBasicImage);
Picasso.with(context).load(imageUri).into(ivBasicImage);


No comments:

Post a Comment

Free Picture Puzzle Jigsaw Puzzle Game - best for kids learning

Picture Puzzle Jigsaw Puzzle Game for free