title: DOwnload base64 image on a protected site using Picasso
body: <p>I tried to use this <a href="https://stackoverflow.com/questions/24273783/android-picasso-library-how-to-add-authentication-headers">Android Picasso library, How to add authentication headers?</a> to access a protected image that returns the base64 version of the image. My problem is that the picasso always failed. and I don't know why. the authorization code is valid since the profile details are loaded. only the image was not. Here is my implementation how to get the image. </p>
  
  <pre><code>public class PicaAuth {
  
  
          private static Picasso sPicasso;
  
          private PicaAuth() {
          }
  
          public static Picasso getImageLoader(final Context context) {
              if (sPicasso == null) {
                  Picasso.Builder builder = new Picasso.Builder(context);
                  builder.downloader(new CustomOkHttpDownloader(context));
                  sPicasso = builder.build();
              }
              return sPicasso;
          }
  
          private static class CustomOkHttpDownloader extends OkHttpDownloader {
  
              public CustomOkHttpDownloader(Context context) {
                  super(context);
              }
  
              @Override
              protected HttpURLConnection openConnection(final Uri uri) throws IOException { 
                  HttpURLConnection connection = super.openConnection(uri);
                  connection.setRequestProperty("Authorization", Auth.getBearerAccessToken());
                  return connection;
              }
          }
      }
  </code></pre>
  
  <p>Main Activity</p>
  
  <pre><code>PicaAuth.getImageLoader(MainActivity.this)
                  .load(uri)
                  .into(mImage, new com.squareup.picasso.Callback() {
                      @Override
                      public void onSuccess() {
                          Log.d("Image Success");
                      }
  
                      @Override
                      public void onError() {
                          Log.e("Image Failed");
                      }
                  });
  </code></pre>