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
- Add an CircularImageView in the XML layout.
- Get the instance of ImageView in Activity class.
- Add Picasso library in your project Gradle module
- 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
- Drawable: Android Loading image from drawable image into an ImageView
- URL: To load an image from URL, the URL is enclosed as a String inside the
load()
method - Error: An error drawable is generally used in cases where there’s a failure in loading the image. inside
.error()
method. - 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(); } });
- Resize: Picasso allows to resize the image before displaying an ImageView by invoking
resize()
method and passing the width and height in pixels - Rotate: To rotate an image pass the float value inside the
rotate()
method. - Scale: Resizing an image can cause stretched images. To keep the aspect ratio intact use
centerCrop()
orcenterInside()
with theresize()
method.
fit()
is like a delayed resize(), that reduces the image to fit the ImageView bounds. - centerCrop and centerInside can’t be used without calling
resize()
with a positive width and height - 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