从android中的URL加载图像

时间:2017-03-03 09:52:47

标签: android android-volley android-imageview

我正在尝试从我从Volley JSONObjectRequest获取的URL加载图像。

这是我的JSON请求:

    //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);
}

这就是我用来从网址加载图片的内容:

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);
    }
}

我继续得到以下异常:

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)

任何人都可以帮我解决我的错误吗?或者即使有更简单的方法来实现这一目标,也许可以使用凌空或其他东西?

5 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

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

检查您的网址,其中包含图片名称之间的空格,尝试使用网址编码

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

尝试使用此网址告诉我。

只需用%20

替换您的空间

答案 2 :(得分:0)

您应编码将处理问题的URL。一种方法是

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

答案 3 :(得分:0)

使用Glide,这是最好的第三方库之一

https://github.com/bumptech/glide

答案 4 :(得分:-1)

您也可以选择使用ImageLoader作为强大的图像加载和缓存工具。

Gradle依赖:

编译'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

使用:

ImageLoader imageLoader = ImageLoader.getInstance(); //获取单例实例

imageLoader.displayImage(imageUri,imageView);

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