【问题标题】:How to load image from aws with picasso with private access如何使用具有私人访问权限的毕加索从 aws 加载图像
【发布时间】:2018-03-07 10:43:58
【问题描述】:

我正在尝试使用 Picasso 将存储在 aws S3 上的图像加载到我的 android 应用程序中,但我的 logcat 中没有任何错误的空白图像,并且对相关代码行的一般调试对我没有任何影响。 我们对图像进行了私人访问,因此图像 url 无法在浏览器上使用。 我需要使用毕加索将图像显示到我的 android 应用程序中。但它不起作用。

下面是我的代码sn-p

  new Picasso.Builder(getApplicationContext()).downloader(new S3Downloader(getApplicationContext(), s3Client, bucket))
                .build()
                .load("https://s3-ea-east-8.amazonaws.com/music/MusicApp_3.jpg")
                .placeholder(R.drawable.img_placeholder)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .networkPolicy(NetworkPolicy.NO_CACHE)
                .into(imageView);

通过使用上面的代码图像仅在安装应用程序后第一次显示。下次它只显示占位符图像

我正在使用this library 来显示图像。

问题不在于毕加索,而在于从“私人”网址加载图像。

请提出解决方案

【问题讨论】:

  • 检查您的图片网址...它找不到服务器...。
  • @VishalVaishnav 我在这个问题中添加了虚拟网址。我无法分享原始网址。
  • 你测试过公共访问吗?
  • 尝试将错误侦听器附加到毕加索并检查您遇到的错误

标签: android amazon-s3 picasso


【解决方案1】:

只需使用这个:

Picasso.with(getApplicationContext()).load(your_url).noFade().into(imageView);

【讨论】:

  • 这不能回答 OP 的问题。问题不在于毕加索,而在于从“私人”网址加载图像。
【解决方案2】:
write below code to load image in Picasso. 
variables:-  
String file_path                          -->> this is your image file path 
Imageview mViewHolder.img_post_photo      -->> this is your imageview to load image.
                        Picasso.with(context)
                                .load(file_path)
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .into(mViewHolder.img_post_photo, new Callback() {
                                    @Override
                                    public void onSuccess() {

                                    }

                                    @Override
                                    public void onError() {
                                        Picasso.with(context)
                                                .load(file_path)
                                                .placeholder(R.mipmap.ic_launcher)
                                                .error(R.mipmap.ic_launcher)
                                                .into(mViewHolder.img_post_photo);
                                    }
                                });
Set dependencies in your app build.gradle file:-
compile 'com.squareup.picasso:picasso:2.5.2'

hope this code helps you.

【讨论】:

【解决方案3】:

您需要从 S3 客户端生成一个预签名的 Url,然后您可以将该 url 传递给 picasso。 该网址将是公开的,并且会有一个到期日期。

【讨论】:

    【解决方案4】:

    您可以使用下面的代码集,这是一个 aynctask,它将使用 glide 通过生成 presignedurl 来附加来自 s3 的私有图像。

      public class sets3imageasync extends AsyncTask<Object, Void, String> {
        // The list of objects we find in the S3 bucket
    
    static final String s3bucket=bucket_name;
    
    
    Context scontext;
    ImageView image;
    
    
    @Override
    protected String doInBackground(Object... objects) {
        // Queries files in the bucket from S3.
        Calendar cal = Calendar.getInstance();
        Log.d(TAG, "doInBackground: in progress");
        cal.setTime(new Date());
        cal.add(Calendar.HOUR, +1);
        Date oneHourLater = cal.getTime();
        AmazonS3Client s3 =  (AmazonS3Client) objects[0];
        scontext= (Context) objects[1];
        image=(ImageView) objects[2];
        String imagekey=(String) objects[3]; 
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(s3bucket, imagekey)
                        .withMethod(HttpMethod.GET)
                        .withExpiration(oneHourLater);
        URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
    
        Log.e("url was", url.toString());
        return url.toString();
    }
    
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    
    
        Log.d(TAG, "onPostExecute: completed "+s);
    
        Glide.with(scontext).load(s)
                .apply(new RequestOptions()
                        .centerCrop())
                .placeholder(scontext.getResources().getDrawable(R.drawable.progress_animation))
                .into((image));
    
    }
    

    这可以如下调用

    new sets3imageasync().execute(s3Client,context,Image_view);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 2014-09-26
      • 2020-09-03
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2021-04-20
      相关资源
      最近更新 更多