Search code examples
androidcardslib

Sending HTTP Request to get photo in cardslib


I'm trying to get photos from secure domain. In order to be able to get the photo, I've to send HTTP request - or send Cookie - to get the photo.

This code is not working, as it sends normal request.

CardThumbnail thumb = new CardThumbnail(getActivity());
String photoUrl = "https://secure.website/image.png";
thumb.setUrlResource(photoUrl);

What I need is getting this secure photo by sending the required cookie.

In retrofit, I used to use:

request.addHeader("Cookie", "SESSION=123; CSRF_TOKEN=" + token);

But what is the solution in cardslib?


Solution

  • In this case the built-in function doesn't work.

    However you can use Picasso to obtain it.For example something like this:

     MyThumbnail thumbnail = new MyThumbnail(mContext);
     //You need to set true to use an external library
     thumbnail.setExternalUsage(true);
     addCardThumbnail(thumbnail);
    
    
     public class MyThumbnail extends CardThumbnail {
    
        @Override
        public void setupInnerViewElements(ViewGroup parent, View viewImage) {
    
            //Here you have to set your image with an external library
            Picasso.Builder builder = new Picasso.Builder(getContext());
            builder.downloader(new OkHttpDownloader(ctx) {
                 @Override
                   protected HttpURLConnection openConnection(Uri uri) throws IOException {
                      HttpURLConnection connection = super.openConnection(uri);
    
                      //Put your header values here 
                      connection.setRequestProperty("X-HEADER", "VAL");
    
                      return connection;
                 }
            });          
            Picasso sPicasso = builder.build();
            sPicasso.load("https://secure.website/image.png")
                   .into((ImageView)viewImage);
    
        }
    
     }
    

    If you would like to use the UniversalImageLoader library, you can integrate the library in a similar way, as described here:

    https://github.com/gabrielemariotti/cardslib/blob/master/doc/OTHERLIBRARIES.md#using-card-with-android-universal-image-loader

    To add parameters to the header in UIL, you can refer this question on SO:

    Is there a way to specify extra headers while fetching images using Universal Image Loader?