If you are developing a live android application then you will surely need to load image from URL or internet and set it into ImageView. You will find various methods to do this, but all those methods are difficult to understand. In this tutorial I am sharing the simplest way to fetch image from internet.
For doing this we first create an object of URL class and pass this object to decodeStream() method of BitmapFactory class. decodeStream() method decode an input stream into a bitmap. Finally we set this bitmap image into ImageView using setImageBitmap() method.
You should always keep in mind that network related work must be done using AsyncTask class. It is a class used to perform task in background. We will show a progress dialog until the image is loading.
Android Load Image from URL (Internet) Example
Create a new project with application name Android Example and package name thecrazyprogrammer.androidexample
Now add following code in respective files.
activity_main.xml
<LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Load Image" android:id="@+id/button" android:onClick="buttonAction"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/image"/> </LinearLayout>
MainActivity.java
package thecrazyprogrammer.androidexample; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.InputStream; import java.net.URL; public class MainActivity extends Activity { ImageView image; Button button; ProgressDialog progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image=(ImageView)findViewById(R.id.image); button=(Button)findViewById(R.id.button); } public void buttonAction(View view) { new LoadImage().execute(); } public class LoadImage extends AsyncTask <Void,Void,Bitmap> { @Override protected Bitmap doInBackground(Void... params) { Bitmap bitmap=null; try { bitmap= BitmapFactory.decodeStream((InputStream)new URL("http://thecrazyprogrammer.com/wp-content/uploads/2015/07/The-Crazy-Programmer.png").getContent()); } catch (Exception e) { e.printStackTrace(); } return bitmap; } @Override protected void onPreExecute() { //show progress dialog while image is loading progress=new ProgressDialog(MainActivity.this); progress.setMessage("Loading Image...."); progress.show(); } @Override protected void onPostExecute(Bitmap bitmap) { if(bitmap!=null) { image.setImageBitmap(bitmap); progress.dismiss(); } else { progress.dismiss(); Toast.makeText(MainActivity.this,"Some error occurred!",Toast.LENGTH_LONG).show(); } } } }
As we are using internet so we need to add internet access permission. Add following line of code to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="thecrazyprogrammer.androidexample" > <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
We have used an AsyncTask here to perform the loading operation. AsyncTask make your code a bit longer. If you want to simply it more you can use a 3rd party libraries like volley or picasso for android.
In case you are facing any difficulty or have any doubts regarding above android load image from url tutorial then feel free to comment below.