Señor Reginold Francis Señor Reginold Francis - 6 months ago 96
Android Question

Android Drawable Images from URL

I am presently using the following piece of code to load in images as drawable objects form a URL.

Drawable drawable_from_url(String url, String src_name)
throws, {
return Drawable.createFromStream(((, src_name);


This code works exactly as wanted, but there appears to be compatibility problems with it. In version 1.5, it throws a
when I give it a URL. In 2.2, given the exact same URL, it works fine. The following URL is a sample input I am giving this function.

How would I load in images in a way that is compatible across the board from a URL?

Answer Source

Solved it myself. I loaded it in as a bitmap using the following code.

Bitmap drawable_from_url(String url) throws, {

    HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();

    InputStream input = connection.getInputStream();

    return BitmapFactory.decodeStream(input);

It was also important to add in the user agent, as googlebooks denies access if it is absent

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download