Google Programming Environment: How to get a Bitmap from a URL

Google Programming Environment: How to get a Bitmap from a URL
Page content

Class Introductions

Before starting with the code, let’s have a look at the classes we are going to use. This will ease us into the process. I will do this for the rest of our lessons.

URL

With this class we can specify the location of a resource on the Internet. In our case we are looking for images.

HttpURLConnection

This class is responsible for establishing a connection to a URL using HTTP. With this class we will connect with the URL.

BitmapFactory

With this class, we can create Bitmaps from different sources, from byte-arrays, files or data streams. This will be useful in our code to get the stream from the http connection and convert it to a Bitmap.

InputStream

This class help us to read bytes from a stream, in our case from a http connection, but it can be used in many other cases.

Bitmap

This is our image class. Here we will store the image we get from the URL.

Android Code

To make it much more interesting, portable and reusable, let’s create a class, just for this functionality.

Let’s create it from scratch:

public class BmpFromURL {

//The following code will be placed inside here

}

It’s interesting to use names in the classes that helps us to identify what is inside them, or at least their functionality. When we are working in Android, or in general when using Java, it is great to follow some style rules. One of these rules is: Put in uppercase the first letter of a class name. We will see more style rules in further articles. Let’s continue:

We create a Bitmap object. Here will be stored the file we get from the URL

private Bitmap myBitmap;

Now, let’s create the constructor with a String parameter (imageURL), this parameter will be the URL of the image in a string format:

https://www.mysite/images/image.jpg

Public BmpFromURL(String imageURL){

myImageURL is the URL object we are going to create from the String we are passing in the constructor (imageURL).

URL myImageURL = null;

We put a “try-catch” in this piece of code, because we need to ensure that the URL is well formed. In the case that the “imageURL” is not a valid URL, the exception will be launched, giving a error message.

try {

myImageURL = new URL(imageURL);

} catch (MalformedURLException error) {

error.printStackTrace();

}

In the next lines we open the connection to the URL using HttpURLConnection, we open an InputStream and we construct a Bitmap from the input Stream, using the BitmapFactory. This way, we will have our image in the “Bitmap” object.

try {

_HttpURLConnection connection = (HttpURLConnection)_myImageURL .openConnection();

connection.setDoInput(true);

connection.connect();

InputStream input = connection.getInputStream();

myBitmap = BitmapFactory.decodeStream(input);

} catch (IOException e) {

e.printStackTrace();

}

}

We only need to create a “getMyBitmap” method to access from external classes.

public Bitmap getMyBitmap() {

return myBitmap;

}

How to use it?

Using this class is so simple.

Just create an object from class BmpFromURL:

myBmpFromURL = new myBmpFromURL(“https://www.mysite/images/image.jpg").

and call the get method

Bitmap myBitmap = myBmpFromURL.getMyBitmap();

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev

This post is part of the series: How-to Develop Applications on Android: Drawing

In this serie, we will learn how to use Canvas, Bitmaps and other images resources in our Applicacions.

  1. Converting Views to Bitmap Images in Android
  2. Google Programming Environment: How to get a Bitmap from a URL