Привет, я интегрировал эти коды так, что я смогу загружать файл, а не фотографию, выбранную в AmazonS3, но для некоторых частей они все еще являются частью загрузки изображения, например "intent.setType ( "изображение / "); "*
Пожалуйста, дайте мне знать, если что-нибудь может быть изменено на эти коды, чтобы мой файл был успешно загружен в AmazonS3. Большое спасибо.
package 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();
}
}