简体   繁体   English

我可以将位图变量中的图像上传到Android中的Amazon S3吗?

[英]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. 所以,是的,我可以保存位图并使用该新文件上传到Amazon,但是我在内存中会有两倍相同的图像,这不是必需的。 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 首先看一下这个问题的答案: Android:将位图转换为输入流

Then use the version of PutObjectRequest that takes an InputStream. 然后使用带有InputStream 的PutObjectRequest版本

I am late to answer this question but try this out by sending byte[]/InputStream directly.Make sure you provide content length in metadata. 我现在回答这个问题很晚,但是可以直接发送byte [] / InputStream来尝试一下。确保您在元数据中提供内容长度。

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) {
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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