0
//getting access to the photo gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
uploadImage.setImageURI(selectedImage);
}
Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
}
//Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
//Trying to upload image to the AWS S3bucket-
public void uploadImageToAWS(View v) {
//String sdcard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "Camera/";
Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
File MY_FILE = new File("myPhoto.png");
String YOUR_IDENTITY_POOL_ID ="POOL_ID";
CognitoCachingCredentialsProvider credentialsProvider =
new CognitoCachingCredentialsProvider(getApplicationContext(),
YOUR_IDENTITY_POOL_ID,
Regions.US_EAST_1);
AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
TransferUtility transferUtility = new TransferUtility(s3Client,
getApplicationContext());
TransferObserver observer = transferUtility.upload(
"apptestingbucket", // The S3 bucket to upload to
FILE_NAME, // The key for the uploaded object
MY_FILE);// The location of the file to
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if(state == TransferState.COMPLETED){
Toast.makeText(Social.this, "transfer Succeded!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
int percentage = (int) (bytesCurrent/bytesTotal * 100);
Log.e("Status", "Percentage" + percentage);
}
@Override
public void onError(int id, Exception ex) {
Log.e("MY_BUCKET", "Error: " + ex.getMessage());
}
});
}
private void retrieveImageFromAWS(){
File sdcard = Environment.getExternalStorageDirectory();
File MY_FILE = new File(sdcard, "social.png");
String YOUR_IDENTITY_POOL_ID ="POOL_ID";
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(),
YOUR_IDENTITY_POOL_ID
, Regions.US_EAST_1);
AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
TransferUtility transferUtility = new TransferUtility(s3Client,
getApplicationContext());
TransferObserver observer = transferUtility.download("BUCKET_NAME",
"FILENAME_TO_BE_DOWNLOADED", MY_FILE);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if(state == TransferState.COMPLETED){
Toast.makeText(Social.this, "Retrieve completed", Toast.LENGTH_LONG).show();
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
int percentage = (int) (bytesCurrent/bytesTotal * 100);
Log.e("status", "percentage" +percentage);
}
@Override
public void onError(int id, Exception ex) {
Log.e("MY_BUCKET", "Error Downloading: " + ex.getMessage());
}
});
}
これはAndroid s3のドキュメントに基づいており、フォトギャラリーから写真を選んだり電話から写真を撮ったり、S3バケットにアップロードしてアプリのインターフェースに戻したりしようとしています。私はアプリがクラッシュしたプログラムを実行する時間をevry。そして私は、任意の例や少しの助けAWS s3バケットに画像をアップロードするには?
BtnUpload.onClicklistener
insede
uploadImageToAWS(v)
と呼ばいただければ幸いです。ありがとうございました!
1)プログラムがクラッシュしている場合は、logcatを追加します。2)ネットワーク操作を実行するには、おそらくAsyncTaskが必要です –