使用Picasso在受保护的站点上加载base64映像

时间:2014-12-09 08:57:19

标签: android

我尝试使用此Android Picasso library, How to add authentication headers?来访问返回图像的base64版本的受保护图像。我的问题是毕加索总是失败。而且我不知道为什么。授权代码有效,因为加载了配置文件详细信息。只有图像不是。这是我实现如何获取图像。

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

2 个答案:

答案 0 :(得分:3)

您需要拦截答案并进行更改

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

答案 1 :(得分:2)

以上回答非常有效。然后,如果基础64编码图像进一步存储在JSON对象内。

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