【问题标题】:How to download an Image by using Volley?如何使用 Volley 下载图像?
【发布时间】:2016-12-12 16:05:45
【问题描述】:

我正在尝试使用 Volley 库将图像下载到 ImageView。
我将 Volley 库的响应注入到 ImageView 中,但没有得到想要的结果。

请检查我的代码并建议我可以在哪里进行更改以获得所需的结果。

public class MainActivity extends AppCompatActivity {
    Button response_click;
    TextView text_response;
    RequestQueue requestQueue;
    ImageView image_download;
    String server_url="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Starburst_in_NGC_4449_(captured_by_the_Hubble_Space_Telescope).jpg/1024px-Starburst_in_NGC_4449_(captured_by_the_Hubble_Space_Telescope).jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        response_click=(Button) findViewById(R.id.click_response);
        text_response=(TextView) findViewById(R.id.text_response);
        image_download=(ImageView)findViewById(R.id.image_download);
    }
    public void response_click(View view){
        requestQueue= Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest=new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                image_download.setImageResource(Integer.parseInt(response));


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                text_response.setText("ou got an error...");
            }
        });

    }
}

【问题讨论】:

    标签: android android-volley android-imageview


    【解决方案1】:

    您使用了错误的请求类型。有一个ImageRequestImageLoader

    请参考an example snippet in the docs或以下示例:

    ImageView mImageView;
    String url = "http://i.imgur.com/7spzG.png";
    mImageView = (ImageView) findViewById(R.id.myImage);
    ...
    
    // Retrieves an image specified by the URL, displays it in the UI.
    ImageRequest request = new ImageRequest(url,
        new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                mImageView.setImageBitmap(bitmap);
            }
        }, 0, 0, null,
        new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
                mImageView.setImageResource(R.drawable.image_load_error);
            }
        });
    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(request);
    

    或者您也可以使用 NetworkImageView,它也是 Volley 的一部分。

    但是,请注意 Google 有 deprecated their own Volley by phasing out Apache,但有一个临时解决方法:How to use the legacy Apache HTTP client on Android Marshmallow?

    此外,现在有了 P+,如果您的应用使用 Google Maps SDK,您还需要添加 uses-library/false attribute

    但就像大多数人会建议的那样,如果可以的话,请使用 Picasso http://square.github.io/picasso/ 或更新的东西。

    【讨论】:

      【解决方案2】:

      使用这个构造函数版本:

      new ImageRequest(url, myResponseListener, maxWidth,
                       maxHeight, scaleType, Config.RGB_565, myErrorListener);
      

      Volley ImageRequest 中未弃用此构造函数。

      【讨论】:

      • 它不允许添加标题..任何线索如何在 kotlin 中做到这一点?
      猜你喜欢
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      相关资源
      最近更新 更多