Home > Tutorials > Grab image from URL

Grab image from URL



Above is the sample result of grabbing an image from URL to display in an ImageView control.

Let’s roll!

Create new Android Project

Project Name: DownloadImageDisplay

Build Target: Android 2.3.3

Application Name: Download Image Display

Package Name: pete.android.study

Create Activity: MainActivity

Min SDK: 10

A – The layout

Pretty much simple:

+ One text view to display the URL of the image, if

+ One image view to display the

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"




B – Coding

package pete.android.study;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	// declare internal using controls
	private TextView txtUrl;
	private ImageView imgView;

    public void onCreate(Bundle savedInstanceState) {

        // set url text
        String url = "http://www.droidviet.com/images/up_img/dalats_com_up12_09_2010_11_091610742910.archos-android.jpg";
        txtUrl = (TextView)findViewById(R.id.txtUrl);

        // load image view control
        imgView =(ImageView)findViewById(R.id.imgView);

        // grab image to display
        try {
        } catch(Exception e) {
        	txtUrl.setText("Error: Exception");

    private Drawable grabImageFromUrl(String url) throws Exception {
    	return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");

C – Note

– In order to make application to be able to connect to Internet and download files, you need to set uses-permission. Open file: AndroidManifest.xml and add this line:

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

– The above code is not the best implementation

– Due to just giving an example of grabbing image, so I don’t care what kind of exception it might catch. You can find it yourselves, it’s pretty much easy to figure out.

D – What I Learned?

– Know how to get an image from an URL (without caching file)

E – Final Words

– The code seems to explain itself, and is very simple.

– Don’t mind to write a comment whatever you like to ask, to know, or to suggest, recommend.

– Hope you enjoy it!


Pete Houston

Categories: Tutorials Tags: , , , ,
  1. July 28, 2014 at 12:14 am

    i am run the code same as given above and app install in emulator successfully but image
    never load in emulator it show error : Exception

  2. Amit Paul
    June 30, 2014 at 5:14 pm

    It is going to generate network on main thread exception. Put the image fetching from url part inside an asynctask to avoid the exception.

  3. March 19, 2014 at 6:59 pm

    not working codee…..

  4. Ramla
    July 26, 2013 at 2:44 am

    Hi tried running your code . I am unable to load the image . The line Drawable.createFromStream((InputStream)new URL(url).getContent(), “src”) throws an exception . I have added internt permision in manifest and internet is also working.

  5. June 20, 2013 at 10:33 pm

    Thanks in support of sharing such a nice thought, piece of writing is nice, thats
    why i have read it completely

  6. June 15, 2013 at 1:11 am

    Incredible quest there. What happened after?
    Take care!

  7. June 14, 2013 at 4:06 am

    I’m not sure exactly why but this website is loading extremely slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check
    back later and see if the problem still exists.

  8. JIthin Raj
    April 7, 2013 at 3:16 pm

    It works,thanks dude…

  9. Choi
    March 24, 2013 at 9:01 pm

    Can I scale the image with its height and width fit to the screen? Thanks

  10. riko
    July 24, 2012 at 2:41 pm

    It doesn’t work with SDK versions 11 and above, any idea why?

  11. March 22, 2012 at 12:21 pm


  12. Adam
    March 16, 2012 at 3:48 am

    Finally a tight code for this url thingee, Great tut.

  13. sushant
    December 6, 2011 at 12:02 pm

    how about setImgeUri with image…

  14. tuan
    November 7, 2011 at 7:20 pm

    I have some problems that is
    how to get a field in if we don’t have their webservice?
    Thank in adv!!

  15. tuan
    November 7, 2011 at 6:54 pm

    Great!!!Lv u guys:D

  16. October 10, 2011 at 12:25 pm

    The stuff works but is there any way in which we can increase the height and width of the image, that we are getting?

    I tried setting that in the xml layout file, but that seems not working

    • October 10, 2011 at 9:59 pm

      Resize the image after downloading, using

  17. Spinnaker
    June 25, 2011 at 1:39 am

    Hi xjaphx,
    Wonderful tutorial. Is there any way that I could save the image in the url. I would like to grab the image and save it onto a database for later use. Thanks.

  1. July 23, 2012 at 5:03 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: