【问题标题】:DOwnload base64 image on a protected site using Picasso使用 Picasso 在受保护的网站上下载 base64 图像
【发布时间】:2015-02-07 02:28:12
【问题描述】:

我尝试使用此 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");
                    }
                });

【问题讨论】:

    标签: android


    【解决方案1】:

    你需要截取答案并改变它

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

    【讨论】:

      【解决方案2】:

      以上答案效果很好。那么如果base 64编码的图像进一步存储在一个JSON对象中。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-14
        • 1970-01-01
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多