Mohammad Rajob
Mohammad Rajob

Reputation: 743

android aws s3 display image

I have an android application. This app can upload image. But when i access it it shows "The connection is untrusted". So how can I display uploaded image skipping this untrusted page. After sometimes when I try to display that image it says access denied. Is there any way to create public url?

Here is my 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;
    }
}
}

Here is the constant 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);
}

}

Here is the untrusted image: enter image description here

Please help me. Sorry for my poor english. Thanks in advance.

Upvotes: 2

Views: 2213

Answers (2)

user2196650
user2196650

Reputation: 47

I was wondering if you still had the Eclipse/Android project files for this still? I'm building something similar and Id like take a look at a working example. I can leave you my email.

It would be greatly appreciated!

Upvotes: 0

Greg Ennis
Greg Ennis

Reputation: 15379

In S3GeneratePresignedUrlTask, your code gets a ticket to the image because the image is stored without public permissions. The ticket is set to expire in one hour so the URL will stop working at that time.

Instead of doing that, grant open/download permission to Everyone when you upload the file. The URL will then be public. To do that, add the canned permission PublicRead in your PutObjectRequest. See the docs here.

To avoid the SSL certificate error just use a http URL instead of https.

Upvotes: 1

Related Questions