我正在使用 Amazon S3 服务 Android API 将图像上传到服务器。当我使用他们的代码再次下载图片时,方法参数要求我传递一个文件来保存这张图片,但我想将这张图片上传到图片 View 。我应该如何将其转换为可绘制对象?
这是方法:
TransferObserver observer = transferUtility.download(
MY_BUCKET, /* The bucket to download from */
OBJECT_KEY, /* The key for the object to download */
MY_FILE /* The file to download the object to */
);
最佳答案
所以你需要在系统上创建一个java.io.File。你可以这样做:
File outputDir = context.getCacheDir();
File imageFile = File.createTempFile("prefix", "extension", outputDir);
然后就可以调用amazon s3代码了:
TransferObserver observer = transferUtility.download(
MY_BUCKET, /* The bucket to download from */
OBJECT_KEY, /* The key for the object to download */
MY_FILE /* The file to download the object to */
);
请确保同时创建您的 TransferListener,以便您知道文件何时下载。
然后你可以像这样将文件转换为位图:
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
mImageView.setImageBitmap(bitmap);
您也可以使用类似 Picasso 或其他图像处理库的工具来提供帮助。他们只需要能够处理文件,因为这也是 S3 将保存的内容。
关于java - 如何从 Amazon S3 下载图像并将其保存到 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39030038/