[英]Where can I find WORKING samples of Amazon's S3 Image Upload Code for Android
[英]Can I upload an image in a bitmap variable to Amazon S3 in Android?
我正在开发一个需要将图片上传到亚马逊的应用程序。 我这样上传:
s3Client.putObject( new PutObjectRequest(myBucket, myKey, myFile) );
就像一个魅力。 问题是现在我需要在上传图片之前对其进行压缩。 我找到了这个:
Bitmap original = BitmapFactory.decodeStream(...);
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out);
Bitmap compressed = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
不过,这仍然需要测试。 所以,是的,我可以保存位图并使用该新文件上传到Amazon,但是我在内存中会有两倍相同的图像,这不是必需的。 如果可能的话,我也不想替换原件,因为以后可能要使用它。 我想我可以创建一个临时文件然后再删除它,但是如果有直接的方法可以上传位图变量,那就太好了。
谢谢你的帮助
首先看一下这个问题的答案: Android:将位图转换为输入流
然后使用带有InputStream 的PutObjectRequest版本 。
我现在回答这个问题很晚,但是可以直接发送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.