Skip to content Skip to sidebar Skip to footer

Unable To Load Image From Https Url In Android

Till this date my app was using the images from an http site and the site is now being upgraded to https . So here comes the issue, now my app does not shows the images at al even

Solution 1:

  {

     trustEveryone();
    // UNIVERSAL IMAGE LOADER SETUPDisplayImageOptionsdefaultOptions=newDisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(newFadeInBitmapDisplayer(300)).build();

    ImageLoaderConfigurationconfig=newImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(newWeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);

    //your image urlStringurl="https://thepoprewards.com.au/uploads/user_image/1514370542.jpg";

    ImageLoaderimageLoader= ImageLoader.getInstance();
    DisplayImageOptionsoptions=newDisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisc(true).resetViewBeforeLoading(true)
            .build();


    imageLoader.displayImage(url, img, options);
}

After write this code now write the method trustEveryone

privatevoidtrustEveryone() {
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(newHostnameVerifier(){
            publicbooleanverify(String hostname, SSLSession session) {
                returntrue;
            }});
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[]{newX509TrustManager(){
            publicvoidcheckClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {}
            publicvoidcheckServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {}
            public X509Certificate[] getAcceptedIssuers() {
                returnnew X509Certificate[0];
            }}}, newSecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(
                context.getSocketFactory());
    } catch (Exception e) { // should never happen
        e.printStackTrace();
    }
}

Solution 2:

use HttpsURLConnection instead of HttpURLConnection for https connections


Update: I just checked the url of your image and it returned status 304 instead 200

Request URL:https://googledrive.com/host/0B_DiX4MiMa3HTHdiYVRmUHBMcW8/image1.jpg Request Method:GET Status Code:304 Not Modified

==> Try handling the status HTTP_NOT_MODIFIED instead of HTTP_OK. Or just handle both cases:

if (httpConnection.getResponseCode() == HttpsURLConnection.HTTP_OK ||
    httpConnection.getResponseCode() == HttpsURLConnection.HTTP_NOT_MODIFIED ) {

      stream = httpConnection.getInputStream();
} else { // just incase..
      log.wtf("Surprize!", "HTTP status was: " + httpConnection.getResponseCode());
}

see documentation about http status 304 Not Modified:

If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.

Post a Comment for "Unable To Load Image From Https Url In Android"