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