Android Volley API imageRequest无效

时间:2018-01-22 10:47:01

标签: android android-volley

代码是:

ImageView imageView = (ImageView) findViewById(R.id.imgView);
ImageRequest ir = new ImageRequest("http://i.imgur.com/7spzG.png",
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap response) {
                            imageView.setImageBitmap(response);
                        }
                    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Image Load Error: ");
                }
            });

此代码无效,错误消息也未显示在logcat中。

2 个答案:

答案 0 :(得分:1)

您错过了将图像请求添加到排球请求队列

 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    final ImageView imageView = (ImageView) findViewById(R.id.imgVw);
    ImageRequest ir = new ImageRequest("http://i.imgur.com/7spzG.png",
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    imageView.setImageBitmap(response);
                }
            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Image Load Error: ");
        }
    });

    requestQueue.add(ir);

答案 1 :(得分:0)

  RequestQueue requestQueue = Volley.newRequestQueue(mContext);
  ImageRequest imageRequest = new ImageRequest(
                        mImageURLString, // Image URL
                        new Response.Listener<Bitmap>() { 
                            @Override
                            public void onResponse(Bitmap response) {
                                // Do something with response
                                imageView.setImageBitmap(response);
                            }
                        },
                        0, // Image width
                        0, // Image height
                        ImageView.ScaleType.CENTER_CROP, // Image scale type
                        Bitmap.Config.RGB_565,
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // Do something with error response
                                error.printStackTrace();
                                Snackbar.make(mCLayout,"Error",Snackbar.LENGTH_LONG).show();
                            }
                        }
                );

                // Add ImageRequest to the RequestQueue
                requestQueue.add(imageRequest);
            }

请检查此代码。如果收到任何错误,它将显示在吐司中。

请参阅This link以获取更多信息。

快乐编码:)