Android Load Image from URL with Internet using BitmapFactory and ImageView.setImageBitmap() Example
August 24, 2015
This page covers Android load image from URL with Internet using BitmapFactory and ImageView.setImageBitmap() example. To work with internet we need a separate thread otherwise we will get android.os.NetworkOnMainThreadException. To handle this situation, we can use AsyncTask. Optionally we can also use StrictMode.ThreadPolicy. In our layout, we need to create a tag for ImageView. To work with internet, we need permission in AndroidManifest.xml as follows.
<uses-permission android:name="android.permission.INTERNET"/>
Load Image using AsyncTask
To load image withAsyncTask
, we need to create a sub class of it where we will define
doInBackground()
and onPostExecute()
. doInBackground()
loads the image and once loaded it is set to ImageView
within onPostExecute()
method. Find the class.
AsyncTaskLoadImage.java
package com.concretepage; import java.io.IOException; import java.io.InputStream; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import android.widget.ImageView; public class AsyncTaskLoadImage extends AsyncTask<String, String, Bitmap> { private final static String TAG = "AsyncTaskLoadImage"; private ImageView imageView; public AsyncTaskLoadImage(ImageView imageView) { this.imageView = imageView; } @Override protected Bitmap doInBackground(String... params) { Bitmap bitmap = null; try { URL url = new URL(params[0]); bitmap = BitmapFactory.decodeStream((InputStream)url.getContent()); } catch (IOException e) { Log.e(TAG, e.getMessage()); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { imageView.setImageBitmap(bitmap); } }
Load Image using StrictMode.ThreadPolicy
We can also useStrictMode.ThreadPolicy
just before calling ImageView.setImageBitmap()
that avoids android.os.NetworkOnMainThreadException
. Find the code snippet.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
Main Activity
MainActivity.javapackage com.concretepage; import java.io.IOException; import java.io.InputStream; import java.net.URL; import android.app.Activity; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private final static String TAG = "MainActivity"; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView)findViewById(R.id.image); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } public void showImageOne(View v) { String url = "https://www.gstatic.com/webp/gallery3/1.sm.png"; new AsyncTaskLoadImage(imageView).execute(url); } public void showImageTwo(View v) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); try { URL url = new URL("https://www.gstatic.com/webp/gallery/4.sm.jpg"); imageView.setImageBitmap(BitmapFactory.decodeStream((InputStream)url.getContent())); } catch (IOException e) { Log.e(TAG, e.getMessage()); } } }
Other XML Files
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.concretepage" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16"/> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup ="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:id="@+id/myLayout"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnmsg1" android:onClick="showImageOne"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnmsg2" android:onClick="showImageTwo"/> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:gravity="center" android:padding="32dp" android:contentDescription="@string/imgDesc"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Concretepage</string> <string name="imgDesc">Image Description</string> <string name="btnmsg1">Click to Show Image One</string> <string name="btnmsg2">Click to Show Image Two</string> </resources>
Output
1. Click on button Click to Show Image One
