简体   繁体   中英

Can I upload an image in a bitmap variable to Amazon S3 in Android?

I am working on an app that needs to upload pictures to Amazon. I am uploading like this:

s3Client.putObject( new PutObjectRequest(myBucket, myKey, myFile) );

Which works like a charm. The thing is that now I need to compress the picture before uploading it. I found this:

Bitmap original = BitmapFactory.decodeStream(...);
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out);
Bitmap compressed = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

That still needs to be tested though. So yeah, I could save the bitmap and use that new file to upload to Amazon, but I would have twice the same image in memory which is not required. I dont want to replace the original either if possible since I might want to use it later. I guess I could create a temporary file and delete it afterwards, but if there is a direct way to upload the bitmap variable, that would be great.

Thanks for any help

First look at the answer to this question: Android: Transform a bitmap into an input stream

Then use the version of PutObjectRequest that takes an InputStream.

I am late to answer this question but try this out by sending byte[]/InputStream directly.Make sure you provide content length in metadata.

private static void uploadToS3(final String OBJECT_KEY, final byte[] bis)  {
        InputStream is = new ByteArrayInputStream(bis);

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType("image/jpeg");
        /*Out of memory can be caused if you don't specify minimum metadata of Content Length of your inputstream*/
        Long contentLength = Long.valueOf(bis.length);
        metadata.setContentLength(contentLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest("BUCKET_NAME",
                OBJECT_KEY,/*key name*/
                is,/*input stream*/
                metadata);
        try {
            PutObjectResult putObjectResult = s3.putObject(putObjectRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Error Message: " + ace.getMessage());
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM