Android Tutorial: Download image from the Internet with URL

6 07 2013

Here is the method that downloads image from the Internet. It gets image URL as a param and returns Bitmap containing an image. When there is a download problem, it throws an Exception. Simple, yet useful :)

The downloader method

public Bitmap getBitmapFromURL(String imageUrl) {
	try {
		URL url = new URL(imageUrl);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		InputStream input = connection.getInputStream();
		Bitmap myBitmap = BitmapFactory.decodeStream(input);
		return myBitmap;
	} catch (IOException e) {
		return null;


As every network operation in Android, this one also requires internet access permissions in AndroidManifest.xml:

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

Sample URL

You can download image from any URL. For example you can take this one:

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



9 responses

28 10 2013

Thanks for the tutorial, but it crash at connection.connect(); in my app :/
I’ve already set the internet access permission.. Any ideas ? Thanks

29 10 2013

Not without the stacktrace. Look there and find the exception. Maybe you are connecting in UI thread? It is prohibited

18 03 2014

put inside asynctask

18 03 2014
Jacek Milewski

definetely should not be fired in main thread as any other network operation

8 07 2014

if android.os.networkonmainthreadexception is occured then add below lines in your class:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();


8 09 2014

thanks for tutorial. but when I save image in sd card, it not supports. please help me

8 09 2014
Jacek Milewski

heh… I truly love such questions in comments :D

What do you expect me to respond on a question like this? Well, if it ‘not supports’ then make it support :D

31 08 2015

Hello Jacek, Thanks for your tutorials. I just implemented a download from url using an AsyncTask. See

What would you say are the advantages of AsyncTask. I’m not 100% sure how the user experiences the 2 methods differently.

5 09 2015
Jacek Milewski

AsyncTask or separate Thread is a must when downloading resources from the internet. In fact the idea behind those is the same the difference is just the API

Give Your feedback:

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

You are commenting using your 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: