Load image inside ImageView from HTTP URL in android using AsyncTask

Easiest Dynamic method to download and load image from http URL and set inside imageview example tutorial.

Loading image through http URL  is very easy with the use of AsyncTask android class. So in this tutorial we are loading our website’s logo inside imageview using AsyncTask class. So here is the complete step by step tutorial for Load image inside ImageView from HTTP URL in android using AsyncTask.

android-project-download-code-button

How to Load image inside ImageView from HTTP URL in android using AsyncTask.

Note: Please add internet permission on AndroidManifest.xml file.

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

Code for MainActivity.java file.

 package com.loadimagehttpurl_android_examples.com;

import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
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;

public class MainActivity extends Activity {
 Button ImageLoadButton;
 ImageView SetImageViewHolder;
 Bitmap Imagebitmap;
 String ImageUrl = "https://www.android-examples.com/wp-content/uploads/2015/11/logo-2015-new-2016.png";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 ImageLoadButton = (Button)findViewById(R.id.button1);
 SetImageViewHolder = (ImageView)findViewById(R.id.imageView1);
 ImageLoadButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View arg0) {
 // TODO Auto-generated method stub
 new ImageLoaderClass().execute(ImageUrl);
 }
 });
 
 
 }
 private class ImageLoaderClass extends AsyncTask<String, String, Bitmap> {
 @Override
 protected void onPreExecute() {
 super.onPreExecute();
 
 }
 protected Bitmap doInBackground(String... args) {
 try {
 Imagebitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
 
 } catch (Exception e) {
 e.printStackTrace();
 }
 return Imagebitmap;
 }
 
 protected void onPostExecute(Bitmap image) {
 
 if(image != null){
 SetImageViewHolder.setImageBitmap(image);
 
 }
 }
 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout 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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.loadimagehttpurl_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="147dp"
 android:src="@drawable/ic_launcher"
 android:background="#000301" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/imageView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="19dp"
 android:text="Click here to load image from URL " />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.loadimagehttpurl_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
 <application
 android:allowBackup="true"
 android:icon="@drawable/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>

Screenshot:

load image

Load image inside ImageView from HTTP URL in android using AsyncTask

Click here to download Load image inside ImageView from HTTP URL in android using AsyncTask project.

Leave a Reply

Your email address will not be published. Required fields are marked *