50388

How to download an Image by using Volley?

Question:

I am Trying to download an Image into an ImageView by using the Volley Library.<br /> I inject the response of the Volley Library into the ImageView, but I am not getting the desired result.

Please check my code and suggest where I can make the changes to get the desired result.

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

Answer1:

You are using the wrong type of request. There is an ImageRequest or ImageLoader.

Please refer to <a href="https://developer.android.com/training/volley/requestqueue#singleton" rel="nofollow">an example snippet in the docs</a> or the example below:

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

Or you can use NetworkImageView, also part of Volley.

<strong>However</strong>, be advised that Google has <a href="https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-apache-http-client" rel="nofollow">deprecated their own Volley by phasing out Apache</a>, but there is a temp workaround: <a href="https://stackoverflow.com/questions/31653002/how-to-use-the-legacy-apache-http-client-on-android-marshmallow" rel="nofollow">How to use the legacy Apache HTTP client on Android Marshmallow?</a>

<strong>Furthermore</strong>, now with P+, if your app uses Google Maps SDK, you will additionally need to add a <a href="https://developers.google.com/maps/documentation/android-sdk/config#specify_requirement_for_apache_http_legacy_library" rel="nofollow">uses-library/false attribute</a>

But like most people will suggest, if you can, use Picasso <a href="http://square.github.io/picasso/" rel="nofollow">http://square.github.io/picasso/</a> or something newer.

Answer2:

Use this constructor version

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

This constructor isn't deprecated in Volley ImageRequest.

Recommend

  • How to provide objects of the same type? Dagger2
  • Setting attributes of an EditText added dynamically in Android
  • onLeScan never gets called android
  • Store additional information during registration with Firebase in Android
  • How to add an item to a ListView with each Button click
  • Eventbus onMessageEvent not getting called
  • Android: How to open the webview in a new screen
  • Listview - list item selection and initial setting (custom made items)
  • How to Handle Click on Imageview in Custom Listview with image and Textview?
  • Android: How do you create an EditText field in java class
  • I have two activity.I want to Send the data from second activity to previous activity.First activity
  • CloseOptionsMenu doesn't work?
  • Unable to resolve Static method?
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • WPF version of .ScaleControl?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Email format validation in mvc3 view
  • Projection media query: browser support and workarounds?
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Deleting and Updating values from a cusrsor adapter
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • align graphs with different xlab
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • How to stop GridView from loading again when I press back button?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?