繁体   English   中英

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

[英]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上传到serverphone 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.

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