[英]How to download image from aws s3 into imageview?
所以我希望能够从我的 s3 存储桶中获取我的图像并(使用滑行或毕加索)将该图像加载到图像视图中。 (我不想将该图像下载到我的手机中)。
目前我有这个:
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 检索图像并将其插入到图像视图中?
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);
}
});
使用 Glide 或 Picasso 时,您无需担心image
保存位置。 Picasso 或 Glide 负责Caching
因此您不必这样做。
现在,使用 Glide 或 Picasso 将 Image 设置为 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);
}
});
如@Prog所示,您必须使用
amazonS3Client!!.generatePresignedUrl({your_bucket_name}, {your_key_image_file}, {expiration_date_of_url})
生成一个可以与 Picasso 一起使用的 url。
但要小心,您必须仅在第一次生成 url。 保存并在未过期时重新使用。 每次使用aws方法生成一个url,响应都是一个新的url。 所以你不会在缓存中获取你的图像,因为他关联的 url 是另一个。 您将再次进行 http 调用。
对不起,我的英语,希望是有帮助的,托马斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.