0

I'm trying to retrieve some pictures from server in order to populate a GridView with the results. To do so I have a custom adapter in which one I would like to download the picture and set it as Bitmap of a ImageButton widget. I'm using ImageRequest from Volley library and also need Basic Authentication with it, which is the point that I cannot figure out how to do. This is the code of the custom adapter :

public View getView(int position, View convertView, ViewGroup parent) {
    final boolean loaded = false;
    currentMarque = getItem(position);
    String url = "https://uriToServer";

    currentMarque.setImageUrl(url + currentMarque.getId());

    if (convertView == null)
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.brand_gridcell_layout, parent, false);

    currentImageBtn = (ImageButton) convertView.findViewById(R.id.brandImageBtn);

    ImageRequest request = new ImageRequest(currentMarque.getImageUrl(),
            new Response.Listener<Bitmap>() 
            {
                @Override
                public void onResponse(Bitmap bitmap) 
                {
                    currentImageBtn.setImageBitmap(bitmap);
                }

            }, 0, 0, null,
            new Response.ErrorListener() 
            {

                @Override
                public void onErrorResponse(VolleyError arg0) 
                {

                }

            }){

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<String, String>();
                    String auth = "Basic " + Base64.encodeToString((GlobalVariables.getInstance().getWS_KEY()+":").getBytes(),
                                    Base64.NO_WRAP);
                    headers.put("Authorization", auth);
                    return headers;
                }
            };
            GlobalVariables.getInstance().addToRequestQueue(request); 


    return convertView;
}

The problem is that an ImageRequest is an asynchronous request, we don't know when will we get the response so the ConvertView is returning before the call ends. Is there a way to wait till the request finishes ?

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • I don't know if your issue is the same as mine or not, but you can have a read at it at [Android/Java: how to delay return in a method](http://stackoverflow.com/questions/31602042/android-java-how-to-delay-return-in-a-method). Hope this help! – BNK Aug 10 '15 at 13:05

1 Answers1

0

Instead of using ImageRequest, you should use NetworkImageView. Using the latter you are able to put an image while the image of the corresponding url are coming. And it work perfectly into adapters and use cache as well. Take a look in this example:

Into xml:

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/networkImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="#000000"/>

Into java class:

NetworkImageView _networkImageView = (NetworkImageView) findViewById(R.id.networkImageView);

_networkImageView.setImageUrl(url, AppControllerImage.getInstance().getImageLoader());
_networkImageView.setDefaultImageResId(R.drawable.notfound);
_networkImageView.setErrorImageResId(R.drawable.notfound);

The following website is a little different than the exemplo I posted, but you can get more info: http://cypressnorth.com/mobile-application-development/setting-android-google-volley-imageloader-networkimageview/

Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • Thank you 8 But it appears is I cannot add Authentication Headers... To load on image from my server, I need to authenticate on the server. – Hubert Solecki Aug 10 '15 at 13:50
  • Here `_networkImageView.setImageUrl(url, AppControllerImage.getInstance().getImageLoader());` I'm using imageloader with cache. You can add your own image loader with the headers you need – Lennon Spirlandelli Aug 10 '15 at 14:45