使用毕加索在受保护的站点上加载base64图像 [英] DOwnload base64 image on a protected site using Picasso
本文介绍了使用毕加索在受保护的站点上加载base64图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用此 Android Picasso库,如何添加身份验证标头?以访问受保护的图像,该图像返回该图像的base64版本.我的问题是毕加索总是失败.我不知道为什么.由于已加载配置文件详细信息,因此授权码有效.只有图像不是.这是我的实现如何获取图像.
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;
}
}
}
主要活动
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");
}
});
推荐答案
您需要截取答案并进行更改
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();
这篇关于使用毕加索在受保护的站点上加载base64图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文