简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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