![](/img/trans.png)
[英]How to upload image to AWS S3 and get the image file's S3 bucket url and save to dynamodb all at once - Android
[英]how to upload an image to aws s3 bucket and get the saved url in Android studio using java
我需要將圖像上傳到 android 中的 AWS s3 存儲桶。 我實現了用戶在獲得圖像路徑后從圖庫中拍攝了一張照片,我需要將其保存在存儲桶中並從 android 的存儲桶中獲取 url。
我有訪問密鑰和密鑰以及存儲桶名稱。 我不清楚如何在 android 中實現這一點。 請幫幫我。
// 這是從圖庫中獲取圖像的代碼
private void selectImage() {
final CharSequence[] options = {"Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Upload Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@SuppressLint("LongLogTag")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = this.getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
thumbnail = getResizedBitmap(thumbnail, 400);
Log.w("path of image from gallery......******************.........", picturePath + "");
imageView.setImageBitmap(thumbnail);
BitMapToString(thumbnail);
}
}
}
private void BitMapToString(Bitmap userImage1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
userImage1.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Document_img1 = Base64.encodeToString(b, Base64.DEFAULT);
System.out.println(Document_img1 + "........IMAGE");
}
private Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
有一些設置/配置要做(如該指南中所述),但實際上傳將如下所示:
Amplify.Storage.uploadFile(
"remoteS3Key",
localFile,
result -> Log.i("Demo", "Successfully uploaded: " + result.getKey()),
failure -> Log.e("Demo", "Upload failed", failure)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.