0

Getting java.net.MalformedURLException: Protocol not found while downloading image using Asynctask in android

My code is:

    private Bitmap downloadImage(String url) {

        HttpURLConnection urlConnection = null;
        try {


            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();
            int statusCode = urlConnection.getResponseCode();
            if (statusCode != 200) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException --"+e.toString());
        //   urlConnection.disconnect();

        }
        catch (Exception e) {
            System.out.println("Exception --"+e.toString());
        }
        finally
         {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }


}

Please help. Thanks in advance.

Iamat8
  • 3,888
  • 9
  • 25
  • 35
Arya
  • 1
  • 3

1 Answers1

0

Error:

java.net.MalformedURLException: Protocol not found

You are getting this error because your URL has no protocol component.

It needs http:// or https:// or whatever other protocol you intend.

I hope it helps you.

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Added image path to url like "http://www.domain.com/img/"+url. This solved exception but couldn't get image. While hardcoding url path like "http://www.domain.com/events/img/imgname.jpg" displays the image – Arya Sep 09 '15 at 07:38
  • can u pls post your entire url? – Rajesh Jadav Sep 09 '15 at 07:43
  • Hardcoded url is String x="http://www.ugotechnologies.com/events/img/k14.jpg"; But String img_url="http://www.ugotechnologies.com/events/img/"+url; doesnt display image – Arya Sep 09 '15 at 07:57
  • Now exception is gone so try to check how to get bitmap from url. and answer solves your problem then please upvote and accept it – Rajesh Jadav Sep 09 '15 at 08:06
  • Here is links that helps you . http://stackoverflow.com/questions/8992964/android-load-from-url-to-bitmap and http://stackoverflow.com/questions/11831188/how-to-get-bitmap-from-a-url-in-android – Rajesh Jadav Sep 09 '15 at 08:07