如何从文件的Andr​​oid上传到Amazon S3 [英] How to upload file to Amazon S3 from Android

查看:187
本文介绍了如何从文件的Andr​​oid上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经将这些codeS,这样我就可以上传文件,而不是照片选择AmazonS3,但对于一些地方,他们仍然部分上传图像suchs作为的 intent.setType(图像/ 的);* 请让我知道如果有什么是可以改变这些codeS有我的文件上传到AmazonS3成功。非常感谢你。

Hi, I have integrated these codes such that I will be able to upload file rather than photo selected to AmazonS3, but for some parts, they are still part of uploading image suchs as "intent.setType("image/");"* Please let me know if anything can be changed to these codes to have my file uploaded to AmazonS3 successfully. Thank you very much.

包com.amazonaws.demo;

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

import com.amazonaws.demo.R;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;

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

public class S3UploaderActivity extends Activity {

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

private Button selectFile = null;
private Button showInBrowser = null;

private static final int FILE_SELECTED = 1;

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

    selectFile = (Button) findViewById(R.id.select_file_button);
    selectFile.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Start the image picker.
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, FILE_SELECTED);
        }
    });

    showInBrowser = (Button) findViewById(R.id.show_in_browser_button);
    showInBrowser.setOnClickListener(new View.OnClickListener() {

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

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

                URL url = s3Client.generatePresignedUrl( urlRequest );

                // Display in Browser.
                startActivity( new Intent( Intent.ACTION_VIEW, Uri.parse( url.toURI().toString() ) ) );
            }
            catch ( Exception exception ) {
                S3UploaderActivity.this.displayAlert( "Browser Failure", exception.getMessage() );
            }
        }
    });
}


// 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 FILE_SELECTED:
        if (resultCode == RESULT_OK) {  
            // THe file location of the image selected.
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};


            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

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

                PutObjectRequest por = new PutObjectRequest( Constants.getPictureBucket(), Constants.FILE_NAME, new java.io.File( filePath) );  // Content type is determined by file extension.
                s3Client.putObject( por );
            }
            catch ( Exception exception ) {
                displayAlert( "Upload Failure", exception.getMessage() );
            }
        }
    }
}

// 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( "OK", new DialogInterface.OnClickListener() {
        public void onClick( DialogInterface dialog, int which ) {
            S3UploaderActivity.this.finish();
        }
    } );
    confirm.show().show();                
}

}

推荐答案

您可以使用此的 HTTP://$c$c.google.com/p/android-file-dialog/ 活动,并获得由用户选择的文件

You could use this http://code.google.com/p/android-file-dialog/ activity and get the file selected by user.

这篇关于如何从文件的Andr​​oid上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆