我有一个Android应用程序,其功能是将图像上传到AWS(亚马逊网络服务)S3。当我第一次成功运行此应用程序图像上传时。但是当我第二次上传图片时,我收到了以下错误。我该如何解决这个错误?
这是错误:
这是我的活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// getActionBar().setDisplayShowTitleEnabled(false);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));
setContentView(R.layout.submit);
submit = (Button) findViewById(R.id.buttonsubmit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri selectedImage = Uri.parse(Environment
.getExternalStorageDirectory().getPath()
+ File.separator
+ "Pictures"
+ File.separator
+ "Spike" + File.separator + "cubicasa.jpg");
new S3PutObjectTask().execute(selectedImage);
}
});
}
private class S3PutObjectTask extends AsyncTask<Uri, Void, S3TaskResult> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(SubmitActivity.this);
dialog.setMessage(SubmitActivity.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.
String filepath = Environment.getExternalStorageDirectory()
.toString()
+ File.separator
+ "Pictures"
+ File.separator
+ "Spike" + File.separator + "cubicasa.jpg";
Uri selectedImage = Uri.fromFile(new File(filepath));
// Uri selectedImage =
// Uri.parse("content://media/external/images/media/40894");
String URLLLLLLLLL = selectedImage.toString();
Log.e("uRLLLLLLLLLLLLLL", URLLLLLLLLL);
ContentResolver resolver = getContentResolver();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(resolver.getType(selectedImage));
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(
SubmitActivity.this
.getString(R.string.upload_failure_title),
result.getErrorMessage());
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Uploaded Successfully", Toast.LENGTH_SHORT);
toast.show();
}
}
}
这是我的常数类:
public class Constants {
public static final String ACCESS_KEY_ID = "accesskey";
public static final String SECRET_KEY = "secretkey";
public static final String PICTURE_BUCKET = "picture-bucket5";
public static final String PICTURE_NAME = "NameOfThePicture5";
public static String getPictureBucket() {
return ("bd.dse.test" + ACCESS_KEY_ID + PICTURE_BUCKET).toLowerCase(Locale.US);
}
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:5)
从您的代码:您每次都在创建存储桶,不要那样会导致存储桶重复。使用名称(字符串)创建一次桶,如下面的代码。
s3Client.createBucket("images");
下次在病房中不要在你的代码中调用这个创建存储桶,只需将图像放在该存储桶中就像下面的代码一样。
S3.createObjectForBucket("images", Token, _Image);