![](/img/trans.png)
[英]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.