简体   繁体   English

在Android中将图像上传到Amazon服务器时显示进度条

[英]Display progress bar while uploading image to Amazon server in Android

I am trying to display a progress bar while uploading an image to the Amazon server. 我在将图像上传到Amazon server.时试图显示progress bar Amazon server. Everything works fine, just that the progress bar is not visible when the image is being uploaded to the server. 一切正常,只是将图像上传到服务器时进度条不可见。 This is what I have tried so far. 到目前为止,这是我尝试过的。

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);

  }

This is what I have done so far. 到目前为止,这是我所做的。 While uploading the image to the server the phone screen goes black(nothing works at this time) and returns to normal after like 10 seconds. image上传到serverphone screen变黑(目前无任何作用),并在大约10秒钟后恢复正常。

for showing progessbar 用于显示进度栏

  private ProgressDialog mProgressDialog;

  private void showProgress() {
    if (isShowProgressDialog) {
        mProgressDialog = Util.getProgressDialog(activity);
    }
}

for hidding progress bar 用于隐藏进度栏

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM