android aws s3显示图片

时间:2014-06-12 10:16:21

标签: android amazon-web-services amazon-s3

我有一个Android应用程序。这个程序可以上传图片。但当我访问它时,它显示“连接是不可信的”。那么如何显示上传的图像,跳过这个不受信任的页面。有时当我尝试显示该图像时,它表示拒绝访问。有没有办法创建公共网址?

这是我的活动:

  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;
    }
}
}

以下是常量活动:

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

请帮帮我。抱歉我的英语不好。提前谢谢。

2 个答案:

答案 0 :(得分:1)

S3GeneratePresignedUrlTask中,您的代码会获取图片的凭单,因为图片是在没有公共权限的情况下存储的。故障单设置为在一小时后过期,因此URL将在此时停止工作。

而不是这样做,而是在上传文件时向Everyone授予开放/下载权限。该URL将是公开的。为此,请在PutObjectRequest中添加罐装权限PublicRead。请参阅文档here

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

答案 1 :(得分:0)

我想知道你是否仍然拥有Eclipse / Android项目文件?我正在构建类似的东西,我喜欢看一个有效的例子。我可以留下你的电子邮件。

非常感谢!