2

I tried to use this Android Picasso library, How to add authentication headers? 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.

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

Main Activity

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");
                    }
                });
Community
  • 1
  • 1
Snippet
  • 1,522
  • 9
  • 31
  • 66

2 Answers2

3

You need to intercept the answer and change it

OkHttpClient client;
OkHttpClient.Builder builderOkHttpClient;
builderOkHttpClient = new OkHttpClient.Builder();
        builderOkHttpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder()
                        .build();
                Response response = chain.proceed(newRequest);
                try {
                    MediaType contentType = response.body().contentType();
                    String  base64String = response.body().string().getBytes("UTF-8");
                    base64String  = base64String .replace("data:image/jpeg;base64,", "");
                    byte[] decodedString = Base64.decode(base64String , Base64.DEFAULT);
                    ResponseBody body = ResponseBody.create(contentType, decodedString);
                    response = response.newBuilder().body(body).build();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return response;
            }
        });

 int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(context.getCacheDir(), cacheSize);
        builderOkHttpClient.cache(cache);
        client = builderOkHttpClient.build();
        Application.getAppComponent().inject(this);
        picasso = new Picasso.Builder(context)
                .downloader(new OkHttp3Downloader(client))
                .loggingEnabled(true)
                .indicatorsEnabled(true)
                .listener(new Picasso.Listener() {
                              @Override
                              public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                                  Log.e("PICASSO", "loading image " + uri);
                                  Log.e("PICASSO ERROR", exception.getMessage());
                              }
                          }
                ).build();
Marlen
  • 146
  • 8
2

Above answer works great. Then if the base 64 encoded image is further stored inside a JSON Object.

 String jsonData = response.body().string();
 JSONObject Jobject = new JSONObject(jsonData);
 String base64String = (String) Jobject.get("ImageData");