How to download image from aws s3 into imageview?

ⅰ亾dé卋堺 提交于 2019-12-12 16:29:15

问题


so I want to be able to grab my images from my s3 bucket and (using glide or picasso) load that image into an imageview. (I don't want to download that image into my phone).

currently i have this:

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

Again, how do i retrieve the image from s3 and insert it into an imageview?


回答1:


When using Glide or Picasso, you need not worry about the location where the image will be saved. Picasso or Glide takes care of the Caching so that you don't have to.

Now, all you need to set an Image to an ImageView using Glide or Picasso is an image URL. I think you want to use an image from your S3 bucket, then you need to have an image from your S3 bucket, something like this:

https://a2.muscache.com/im/pictures/9c66dcc5-6d39-43b3-82ec-72e9d119f873.jpg

then all you need to do with your code is:

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



回答2:


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


            }
        });


来源:https://stackoverflow.com/questions/41404527/how-to-download-image-from-aws-s3-into-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!