All CategoriesAndroid App Development

How to Load Image from URL in ImageView in Android

If you want to load an image from your web URL into Android ImageView in your android application, This blog is for you.
This blog gives you a step by step guide to display image from URL with source code for Android Studio.
These are all the steps to create a project in Android Studio.
Step 1.
Open Android Studio and Crate a project named as “ImageFromURL” and click Next.

Step 2.
Select “Phone and Tablet” Option and click Next.

Step 3:
Add an activity as a Blank Activity and click Next.

Step 4 :
Create a new Blank activity as “MainActivity” and click Finish.

Step 5:
Add Image code to activity_main.xml

<ImageView android:id="@+id/image"

Step 6:

Image URL : String image_url = "";

Add Code to for Get Image from Web using BitMap:

try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
return null;

Step 7:
Decodes Image and Scales it to reduce memory consumption
Add decodeFile() to class..

private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;

Step 8 :
Add Intenet Permission to AndroidManifest.xml

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

Step 9 : Run Your Project .
Step 10 : You will get your output like below:

Keep visiting for more updates on Android and other technologies. Also learn how to store image in database by reading the next post.

Suggested Reading

Horizontal ListView Android

Related Popular Courses:


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles