0

I am trying to load an image from a URL i get from a Volley JSONObjectRequest.

This is my JSON request:

    //creates the post request and posts
    JsonObjectRequest getRandomImageURL = new JsonObjectRequest(Request.Method.POST, url_image,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                   Log.d("ServerResponse","ATM ImageURL"+response.toString());
                    try {
                        String image = response.getString("Image");
                        Log.d("Image","ImageURL "+imgLink+image);



                        imgAd = (ImageView) findViewById(R.id.imgAd);
                        String imgURL = imgLink+image;
                        new DownloadImageTask(imgAd).execute(imgURL);




                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response","Error"+ error.getLocalizedMessage());


                }
            }
    ) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-type", "application/json");
            headers.put("Accept", "application/json");
            headers.put("User-Agent", "Mozilla/5.0");
            headers.put("Content-length", dataStream.toString());
            return headers;
        }
        @Override
        public byte[] getBody() {
            try {
                return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                return null;
            }
        }
    };
    Volley.newRequestQueue(this).add(getRandomImageURL);
}

And this is what i use to load the image from the URL:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

I keep on getting the following exception:

W/System.err: java.io.FileNotFoundException:    https://www.lamdacardservices.com/offers/CINEMA Logo.jpg
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
W/System.err:     at java.net.URL.openStream(URL.java:470)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask$override.doInBackground(AtmMapActivity.java:327)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask$override.access$dispatch(AtmMapActivity.java)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask.doInBackground(AtmMapActivity.java:0)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask.doInBackground(AtmMapActivity.java:317)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:818)

Can anyone help me on what i am doing wrong? Or even if there is an easier way to achieve this, maybe using volley or something?

5 Answers 5

1

Try this:

 String temp = "https://www.lamdacardservices.com/offers/CINEMA Logo.jpg";
    temp = temp.replaceAll(" ", "%20");
    URL sourceUrl = new URL(temp);
0

https://www.lamdacardservices.com/offers/CINEMA Logo.jpg

Check your URL it contains space between image name try to use URL Encoding

https://www.lamdacardservices.com/offers/CINEMA%20Logo.jpg

try with this URL and let me know.

Just replace your space with %20

1
  • stills gives me FileNotFound Exception
    – pavlos
    Mar 3, 2017 at 10:17
0

You should encode URL that will handle problems like. One way is

Uri.encode("https://www.lamdacardservices.com/offers/CINEMA Logo.jpg")
0

Use Glide for this, One of the best third party library

https://github.com/bumptech/glide

-1

You may also opt to use ImageLoader as a powerful image loading and caching tool.

Gradle dependency :

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

To use :

ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance

imageLoader.displayImage(imageUri, imageView);

Source: https://github.com/nostra13/Android-Universal-Image-Loader

2
  • he wants to download IMAGE dude. Mar 3, 2017 at 10:58
  • Yup, he's downloading but is actually rendering the downloaded image in the ImageView in the end. bmImage.setImageBitmap(result);
    – owlein
    Mar 3, 2017 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.