Android Open Source - e2e-app-android-aws S3 Uploader Activity






From Project

Back to project page e2e-app-android-aws.

License

The source code is released under:

MIT License

If you think the Android project e2e-app-android-aws listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 */*from w  w  w. j  a  v  a2 s .  c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.amazonaws.demo.s3uploader;

import java.net.URL;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.view.View;
import android.widget.Button;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;


public class S3UploaderActivity extends Activity {
  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  // This sample App is for demonstration purposes only.
  // It is not secure to embed your credentials into source code.
  // DO NOT EMBED YOUR CREDENTIALS IN PRODUCTION APPS.
  // We offer two solutions for getting credentials to your mobile App.
  // Please read the following article to learn about Token Vending Machine:
  // * http://aws.amazon.com/articles/Mobile/4611615499399490
  // Or consider using web identity federation:
  // * http://aws.amazon.com/articles/Mobile/4617974389850313
  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  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.main);

    selectPhoto = (Button) findViewById(R.id.select_photo_button);
    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.show_in_browser_button);
    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;
    }
  }
}




Java Source Code List

com.amazonaws.demo.s3uploader.Constants.java
com.amazonaws.demo.s3uploader.S3UploaderActivity.java