如何从 AWS S3 下载图像并显示在 ImageView 中?

4

我希望能够从我的S3存储桶中获取图像,并使用Glide或Picasso将该图像加载到ImageView中(我不想将该图像下载到我的手机上)。

目前我有以下代码:

 downloadButton = (Button) findViewById(R.id.cognito_s3DownloadButton);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "fromAWS");
                observer = transferUtility.download("zivit-card-images","image1", file);
                Glide.with(getBaseContext()).load("image in byes? or url?").centerCrop().into(myImageView);
            }
        });

再次强调,我该如何从S3中检索图像并将其插入到ImageView中?


你正在将文件引用传递给 transferUtility.download(),这个文件就是转移实用程序将要下载 S3 对象的文件。 - Karthik
3个回答

4
public String downloadimage()

    {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.HOUR, +1);
        Date oneHourLater = cal.getTime();
        AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
        URL url = s3.generatePresignedUrl(
                "testproject12345",
                "first.jpg",
                oneHourLater
        );
        Log.e("url was", url.toString());
        String urlstring = url.toString();
        return urlstring;
    }

catch that function in your activity


download = (Button)findViewById(R.id.download);
        final ImageView imageview = (ImageView) findViewById(R.id.image);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

 download1 = new Uploader(getmain().getCredentialsProvider(),getApplicationContext());


                String url = download1.downloadimage();
                Picasso.with(getApplicationContext())
                        .load(url)
                        .into(imageview);


            }
        });

2

如@Prog所示,您需要使用

amazonS3Client!!.generatePresignedUrl({your_bucket_name}, {your_key_image_file}, {expiration_date_of_url})

生成一个可以与Picasso一起使用的URL。

但要小心,您只需在第一次生成URL时生成它。存储它并在其未过期时重新使用它。 每次使用aws方法生成URL时,响应都是新的URL。因此,您将无法将图像缓存,因为其关联的URL是另一个URL。您将再次进行HTTP调用。

对我的英语表示抱歉, 希望有所帮助, 托马斯


1
使用Glide或Picasso时,您不需要担心image的保存位置。Picasso或Glide会处理Caching,因此您不必担心。
现在,您只需要使用Glide或Picasso设置一个ImageView的图像,就需要一个图像URL。我想您想要使用来自S3存储桶的图像,那么您需要从S3存储桶中获取一张图像,就像这样:
https://a2.muscache.com/im/pictures/9c66dcc5-6d39-43b3-82ec-72e9d119f873.jpg

那么,您只需要做的就是使用您的代码:
downloadButton = (Button) findViewById(R.id.cognito_s3DownloadButton);
   downloadButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
            Glide.with(getBaseContext())
                 .load("https://a2.muscache.com/im/pictures/9c66dcc5-6d39-43b3-82ec-72e9d119f873.jpg")
                 .centerCrop()
                 .into(myImageView);
       }
});

从TransferObserver对象中,我该如何获取图像的URL?一般来说,获取图像S3 URL的方法是什么?您能否在您的代码中包含它? - Dr.Android
1
抱歉,我没有使用S3的经验,你需要自己处理S3的内容,不过,S3图像URL应该与其他图像URL一样,因此以下代码也适用于S3图像。 - Nilesh Singh
没问题,没问题。感谢回复。 - Dr.Android
@Dr.Android,你需要创建签名 URL 吗? - Yajairo87

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接