gpt4 book ai didi

android aws s3 显示图像

转载 作者:行者123 更新时间:2023-11-29 01:41:39 27 4
gpt4 key购买 nike

我有一个安卓应用程序。这个应用程序可以上传图像。但是当我访问它时,它显示“连接不受信任”。那么如何跳过这个不受信任的页面来显示上传的图像。有时当我尝试显示该图像时,它说访问被拒绝。有什么方法可以创建公共(public)网址吗?

这是我的 Activity :

  public class S3UploaderActivity extends Activity {

private AmazonS3Client s3Client = new AmazonS3Client(
new BasicAWSCredentials(Constants.ACCESS_KEY_ID,
Constants.SECRET_KEY));

private Button selectPhoto = null;
private Button showInBrowser = null;

private static final int PHOTO_SELECTED = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));

setContentView(R.layout.activity_main);

selectPhoto = (Button) findViewById(R.id.button1);
selectPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start the image picker.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PHOTO_SELECTED);
}
});

showInBrowser = (Button) findViewById(R.id.button2);
showInBrowser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new S3GeneratePresignedUrlTask().execute();
}
});
}

// This method is automatically called by the image picker when an image is
// selected.
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch (requestCode) {
case PHOTO_SELECTED:
if (resultCode == RESULT_OK) {

Uri selectedImage = imageReturnedIntent.getData();
new S3PutObjectTask().execute(selectedImage);
}
}
}

// Display an Alert message for an error or failure.
protected void displayAlert(String title, String message) {

AlertDialog.Builder confirm = new AlertDialog.Builder(this);
confirm.setTitle(title);
confirm.setMessage(message);

confirm.setNegativeButton(
S3UploaderActivity.this.getString(R.string.ok),
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
}
});

confirm.show().show();
}

protected void displayErrorAlert(String title, String message) {

AlertDialog.Builder confirm = new AlertDialog.Builder(this);
confirm.setTitle(title);
confirm.setMessage(message);

confirm.setNegativeButton(
S3UploaderActivity.this.getString(R.string.ok),
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

S3UploaderActivity.this.finish();
}
});

confirm.show().show();
}

private class S3PutObjectTask extends AsyncTask<Uri, Void, S3TaskResult> {

ProgressDialog dialog;

protected void onPreExecute() {
dialog = new ProgressDialog(S3UploaderActivity.this);
dialog.setMessage(S3UploaderActivity.this
.getString(R.string.uploading));
dialog.setCancelable(false);
dialog.show();
}

protected S3TaskResult doInBackground(Uri... uris) {

if (uris == null || uris.length != 1) {
return null;
}

// The file location of the image selected.
Uri selectedImage = uris[0];

ContentResolver resolver = getContentResolver();
String fileSizeColumn[] = { OpenableColumns.SIZE };

Cursor cursor = resolver.query(selectedImage, fileSizeColumn, null,
null, null);

cursor.moveToFirst();

int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
// If the size is unknown, the value stored is null. But since an
// int can't be
// null in java, the behavior is implementation-specific, which is
// just a fancy
// term for "unpredictable". So as a rule, check if it's null before
// assigning
// to an int. This will happen often: The storage API allows for
// remote
// files, whose size might not be locally known.
String size = null;
if (!cursor.isNull(sizeIndex)) {
// Technically the column stores an int, but cursor.getString
// will do the
// conversion automatically.
size = cursor.getString(sizeIndex);
}

cursor.close();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(resolver.getType(selectedImage));
if (size != null) {
metadata.setContentLength(Long.parseLong(size));
}

S3TaskResult result = new S3TaskResult();

// Put the image data into S3.
try {
//s3Client.createBucket(Constants.getPictureBucket());

PutObjectRequest por = new PutObjectRequest(
Constants.getPictureBucket(), Constants.PICTURE_NAME,
resolver.openInputStream(selectedImage), metadata);
s3Client.putObject(por);
} catch (Exception exception) {

result.setErrorMessage(exception.getMessage());
}

return result;
}

protected void onPostExecute(S3TaskResult result) {

dialog.dismiss();

if (result.getErrorMessage() != null) {

displayErrorAlert(
S3UploaderActivity.this
.getString(R.string.upload_failure_title),
result.getErrorMessage());
}
}
}

private class S3GeneratePresignedUrlTask extends
AsyncTask<Void, Void, S3TaskResult> {

protected S3TaskResult doInBackground(Void... voids) {

S3TaskResult result = new S3TaskResult();

try {
// Ensure that the image will be treated as such.
ResponseHeaderOverrides override = new ResponseHeaderOverrides();
override.setContentType("image/jpeg");

// Generate the presigned URL.

// Added an hour's worth of milliseconds to the current time.
Date expirationDate = new Date(
System.currentTimeMillis() + 3600000);
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
Constants.getPictureBucket(), Constants.PICTURE_NAME);
urlRequest.setExpiration(expirationDate);
urlRequest.setResponseHeaders(override);

URL url = s3Client.generatePresignedUrl(urlRequest);

result.setUri(Uri.parse(url.toURI().toString()));

} catch (Exception exception) {

result.setErrorMessage(exception.getMessage());
}

return result;
}

protected void onPostExecute(S3TaskResult result) {

if (result.getErrorMessage() != null) {

displayErrorAlert(
S3UploaderActivity.this
.getString(R.string.browser_failure_title),
result.getErrorMessage());
} else if (result.getUri() != null) {

// Display in Browser.
startActivity(new Intent(Intent.ACTION_VIEW, result.getUri()));
}
}
}

private class S3TaskResult {
String errorMessage = null;
Uri uri = null;

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public Uri getUri() {
return uri;
}

public void setUri(Uri uri) {
this.uri = uri;
}
}
}

这是持续的 Activity :

public class Constants {

public static boolean isCapturedOk = false;

public static int imagePickerStatus = -1;

public static byte[] imageData;

public static int result = 0;

public static String printUrl = "";

public static String rotationValue = "";

public static String rotationValueForCamera = "";

public static boolean isPrintButtonVisible = true;

protected static byte[] imageData1;

public static final String ACCESS_KEY_ID = "something";
public static final String SECRET_KEY = "something";

public static final String PICTURE_BUCKET = "picture-bucket8";
public static final String PICTURE_NAME = "photo";


public static String getPictureBucket() {
return ("so.so.so"+ ACCESS_KEY_ID + PICTURE_BUCKET).toLowerCase(Locale.US);
}

}

这是不受信任的图像: enter image description here

请帮帮我。对不起我的英语不好。提前致谢。

最佳答案

S3GeneratePresignedUrlTask​​ 中,您的代码获得图像的票证,因为图像的存储没有公共(public)许可。票据设置为一小时后过期,因此 URL 将在那时停止工作。

不要这样做,而是在上传文件时向所有人授予打开/下载权限。该 URL 将公开。为此,请在 PutObjectRequest 中添加固定权限 PublicRead。请参阅文档 here .

要避免 SSL 证书错误,只需使用 http URL 而不是 https。

关于android aws s3 显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24181926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com