1

I am using the Amazon S3 service Android API to upload an image to the server. When I use their code to download the image again, the method parameters requires me to pass a File to save this image to, but I would like to upload this image to an image view. How am I supposed convert this to a drawable?

this is the method:

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 */
);
nilay neeranjun
  • 145
  • 2
  • 14
  • Try generating URL's for your images, then you can easyly get them with Picaso or Glide or whatever into an Imageview -- http://www.bucketexplorer.com/documentation/amazon-s3--how-to-generate-url-for-amazon-s3-files.html – Tasos Aug 19 '16 at 02:12
  • How would i retrieve them? – nilay neeranjun Aug 19 '16 at 02:14
  • 1st sorry about that link, I thought it was by Amazon but its a paid Service to generate Links for your Bucket. Have a look if Amazon provides a free way to generate links. --- Ok so Once you have the link(s) to your images or whatever you use something like Picasso which is easy to use, so it takes in the link(s) and displays it into an imageView. http://square.github.io/picasso/ – Tasos Aug 19 '16 at 02:19
  • Yeah, but Amazon S3 doesn't provide a way to generate links but it allows you to add a keyword to retrieve the file from the server. Here is some more info: http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/s3transferutility.html. Plus, I'm already paying for Amazon services – nilay neeranjun Aug 19 '16 at 02:22
  • are you sure http://stackoverflow.com/questions/7933458/url-to-get-a-file-from-amazon-s3 – Tasos Aug 19 '16 at 02:24
  • This is not under the specific Android API though – nilay neeranjun Aug 19 '16 at 02:27
  • Not sure but if you want to generate URLS give this a try http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURL.html – Tasos Aug 19 '16 at 02:30
  • It says the URL's are only valid for a specified duration, but I would like to be able to store the images permanently – nilay neeranjun Aug 19 '16 at 02:43
  • Consider using Picasso if you don't already and check out this thread for a couple of ideas what to do: http://stackoverflow.com/questions/30963995/how-do-use-use-picasso-library-while-downloading-images-from-amazon-s3 – liminal Aug 19 '16 at 04:17

1 Answers1

3

So you need to create a java.io.File on the system. You can do something like this:

File outputDir = context.getCacheDir();
File imageFile = File.createTempFile("prefix", "extension", outputDir);

Then you can call the amazon s3 code:

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

Make sure to create your TransferListener as well so you know when the file has been downloaded.

Then you can convert the file to a bitmap like this:

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
mImageView.setImageBitmap(bitmap);

You can also use something like Picasso or other image processing libraries to help as well. They just require the ability to work with a file since that is what S3 will save too.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51