In this post we will see how to load Imageview in Android from URL. We will be making an Android app which will load imageview from a given url on clicking a button. To load image from url in Android, code is:
URL url = new URL("ImageURL"); Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
In your Android studio project take a new layout file and edit it as below:
activity_image.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/pbbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" /> <ImageView android:id="@+id/imgURL" android:layout_width="250dp" android:layout_height="250dp" android:layout_margin="5dp" /> <Button android:id="@+id/btnLoad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:onClick="Load" android:text="LOAD" /> </LinearLayout>
This layout file as Android Imageview, button and a progressBar to show the task is running. When the user will click on the button, it will call the Android Async task to load image from given url. After completion, if Bitmap was successfully parsed it will load Bitmap on Android imageview.
Create a new class file with name ImageFromURL.java and edit it as below:
ImageFromURL.java:
package com.parallel.codes; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.ProgressBar; import java.net.URL; public class ImageFromURL extends AppCompatActivity { ImageView imgURL; ProgressBar pbbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); imgURL = findViewById(R.id.imgURL); pbbar = findViewById(R.id.pbbar); pbbar.setVisibility(View.GONE); } public void Load(View v) { LoadImageURL loadImageURL = new LoadImageURL(); loadImageURL.execute(""); } private class LoadImageURL extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { pbbar.setVisibility(View.VISIBLE); imgURL.setVisibility(View.GONE); } @Override protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap bmp = null; try { URL url = new URL("https://parallelcodes.com/wp-content/uploads/2018/06/im-500821018.jpg"); bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); bmp = null; } return bmp; } @Override protected void onPostExecute(Bitmap result) { if (result != null) imgURL.setImageBitmap(result); pbbar.setVisibility(View.GONE); imgURL.setVisibility(View.VISIBLE); } } }
Now add internet permission in AndroidManifest.xml file to fetch URL :
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run the application now.
Also see:
How to create Navigation drawer in Android?
How to play videos from path or gallery in Android?
How to use SharedPreferences in Android?
Android Firebase – How to upload and Download Images using Firebase?
Android Exoplayer – How to load and play videos?