2016-08-19 85 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會節省的。