[英]how to upload an image to aws s3 bucket and get the saved url in Android studio using java
I need to upload the image to the AWS s3 bucket in android.我需要将图像上传到 android 中的 AWS s3 存储桶。 I implemented that the user has taken a photo from the gallery after having the image path I need to save that in a bucket and get the url from bucket in android.
我实现了用户在获得图像路径后从图库中拍摄了一张照片,我需要将其保存在存储桶中并从 android 的存储桶中获取 url。
I HAVE access key and secret key and bucket name.我有访问密钥和密钥以及存储桶名称。 I am not clear how to implement this in android.
我不清楚如何在 android 中实现这一点。 Please help me out.
请帮帮我。
// This is the code of taking an image from the gallary // 这是从图库中获取图像的代码
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);
}
Please check out the Storage category in the AWS Amplify library .请查看AWS Amplify 库中的存储类别。
There's some setup/configuration to do (as noted in that guide), but the actual upload will look like this:有一些设置/配置要做(如该指南中所述),但实际上传将如下所示:
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.