2016-08-19 84 views
1

我正在使用Amazon S3服务Android API将图像上传到服务器。当我使用他们的代码再次下载图像时,方法参数要求我传递一个文件以保存此图像,但是我想将此图像上传到图像视图。我该如何将其转换为drawable?如何将图像从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 */ 
); 
+0

试着为你的图片生成URL,然后你可以很容易地将它们与Picaso或者Glide或者其他任何东西一起放入Imageview中 - http://www.bucketexplorer.com/documentation/amazon-s3-how-to-generate- url-for-amazon-s3-files.html – Tasos

+0

我将如何检索它们? –

+0

对此链接感到抱歉,我认为这是亚马逊,但它的付费服务为您的Bucket生成链接。看看亚马逊是否提供了一种免费的方式来生成链接。 ---好吧,一旦你有链接到你的图像或任何你喜欢的Picasso这是易于使用的东西,所以它需要链接(S)并显示它到一个ImageView。 http://square.github.io/picasso/ – Tasos

回答

3

所以,你需要在系统上创建的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); 

您也可以使用类似毕加索或其他图像处理库,以帮助为好。他们只需要能够使用一个文件,因为这也是S3会节省的。