samedi 19 septembre 2015

MalformedURLExceptionError Android App

We are trying to get an image from a URL but we are getting a Malformed URL exception. The URL appears to be valid and we can access it from a phone. Any ideas as to why the below code doesn't work?

Drawable z;

    String url = "http://ift.tt/1OG0Mfv";


    z = drawableFromUrl(url); 

And the drawableFromUrl method is at follows:

public Drawable drawableFromUrl(String url)throws java.io.IOException {

    try {
        Bitmap x;
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();

        x = BitmapFactory.decodeStream(input);

        Drawable mDrawable = new BitmapDrawable(getResources(), x);

        return mDrawable;
    }
    catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}




Aucun commentaire:

Enregistrer un commentaire