Thursday, September 18, 2014

Load images from Url and Display to ImageView in Android


Hi...Friends..Its always a tedius and combursome task for android begginers to find the way to display images from url to imageView or use them elsewhere,Even I was also searched a lot when i was at that stage,And finally i find a better way to do it without making any bunch of heavy code classes and methods,So without talking much lets learn how to do it.Its really simple with following steps,

STEP:1


First of all create a new Android application project in your Eclipse IDE,name it as you like.

STEP:2


After that go to your layout folder from project explorer,select your xml file in which we will put an imageView.In this imageView we will display our image ,Which will be loaded from url(web).So change your layout xml file as my code given follows,

main.xml


<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:padding="10dp"
tools:context="com.jims.imageloaderdemo.ImageActivity" >

<TextView
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:gravity="center"
android:text="ANDROID MANIA"
android:textColor="#545454"
android:textSize="20dp" />

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/tv_title"
android:layout_centerInParent="true" />

</RelativeLayout>

now,let as proceed to our next step,


STEP:3


Now,for loading an Image from the web using its webURL i found an easiest way,Its nothing but using a free 3rd party library called "UNIVERSAL IMAGE LOADER".It is a very usefull library for loading image from url and also having many features like caching.

So,we can download it from internet and simply put it in our "libs" folder of our project,So all of its classes and methods will be accessible in our project,You can download it from HERE(Universal ImageLoader).

Once you have downloaded it,Put that "UniverasalImageLoader.jar" into your "libs" folder.

STEP:4


Now the original coding part is beggining,But don't worry its really very simple to code and understand.just open your "src" folder,Go to your activity class file and make changes as below code,

MainActivity.java



package com.jims.imageloaderdemo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class ImageActivity extends ActionBarActivity {
ImageView imageView;
private DisplayImageOptions options;
public static ImageLoader imageLoader;
String imageUrl;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);

// YOUR IMAGE URL....
imageUrl = "http://digiguide.tv/i/blog/userfiles/image/hero-android.png";
imageView = (ImageView) findViewById(R.id.image);
//Initialize imageLoade instance
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration
.createDefault(ImageActivity.this));
//Options are for cache and for displaying another image like please wait 
options = new DisplayImageOptions.Builder().cacheOnDisc(true)
.showStubImage(R.drawable.wait)
.showImageOnFail(R.drawable.wait).build();

imageLoader.displayImage(imageUrl, imageView, options);

}

}

So,Its complete here coding.

STEP:5


Upto here we just have completed our coding,But one more thing is to make sure that we must have to give an "Internet permission" in our "manifest.xml " file,If you haven't yet Please make changes as below code in your manifest.xml file.

AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jims.imageloaderdemo"
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=".ImageActivity"
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>




Now,Clean and build your project and run it,



The output will be as below,


Hope,You find the easy way to do it,Thank you have a happy coding..!!! :)

















No comments:

Post a Comment

Time is money so...make more n more mony so u can get more tym of ur lyf...:-P