[英]Display progress bar while uploading image to Amazon server in Android
我在將圖像上傳到Amazon server.
時試圖顯示progress bar
Amazon server.
一切正常,只是將圖像上傳到服務器時進度條不可見。 到目前為止,這是我嘗試過的。
My Activity
cameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Take the picture for the first time.
photoFileName += RandomStringUtils.randomAlphanumeric(5) + ".jpg";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
// progressBar.setVisibility(View.VISIBLE);
if (resultCode == RESULT_OK) {
Uri takenPhotoUri = getPhotoFileUri(photoFileName);
Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
takenImage.compress(Bitmap.CompressFormat.JPEG, 50, bos);
try {
AmazonS3Client s3Client = new AmazonS3Client(
new BasicAWSCredentials(MY_ACCESS_KEY_ID, MY_SECRET_KEY));
byte[] contentBytes = bos.toByteArray();
InputStream is = new ByteArrayInputStream(contentBytes);
Long contentLength = Long.valueOf(contentBytes.length);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLength);
PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME,
photoFileName, is, objectMetadata);
//progressBar.setVisibility(View.VISIBLE);
PutObjectResult result = s3Client.putObject(putObjectRequest);
// progressBar.setVisibility(View.INVISIBLE);
}
到目前為止,這是我所做的。 將image
上傳到server
, phone screen
變黑(目前無任何作用),並在大約10秒鍾后恢復正常。
用於顯示進度欄
private ProgressDialog mProgressDialog;
private void showProgress() {
if (isShowProgressDialog) {
mProgressDialog = Util.getProgressDialog(activity);
}
}
用於隱藏進度欄
private void dismissProgress() {
if (isShowProgressDialog) {
Util.dismissDialog(activity, mProgressDialog);
}
}
public static ProgressDialog getProgressDialog(Context c) {
//ProgressDialog mProgressDialog=new ProgressDialog(c);
MyCustomProgressDialog mProgressDialog = new MyCustomProgressDialog(c);
//mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(isCancelable);
mProgressDialog.show();
return mProgressDialog;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.