当我们上传到Firebase时如何实现ProgressListener

时间:2019-06-06 00:33:38

标签: java android firebase progress-bar firebase-storage

我正在尝试通过android应用将文件上传到Firebase,所以我希望上传进度显示在进度栏中

在使用其他方法TaskSnapshot之前,我已经设法实现了它:progress =(100.0 * taskSnapshot.getBytesTransferred())/ taskSnapshot.getTotalByteCount(),但现在看来我找不到办法

    private void uploadFile(Uri data) {

  progressBar.setVisibility(View.VISIBLE);
  final   StorageReference sRef = 
 mStorageReference.child(Constants.STORAGE_PATH_UPLOADS + 
  System.currentTimeMillis() + ".pdf");
  Task<Uri> urlTask = sRef.putFile(data).continueWithTask(new 
  Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
        @Override
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) 
 throws Exception {
            if (!task.isSuccessful()) {
                throw task.getException();
            }

            // Continue with the task to get the download URL
            return sRef.getDownloadUrl();
        }
    })
          .addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("File Uploaded Successfully");
                Uri downloadUrl = task.getResult();
                String url=downloadUrl.toString();
                Upload upload = new Upload(editTextFilename.getText().toString(),url);
                mDatabaseReference.child(mDatabaseReference.push().getKey()).setValue(upload);

            }
        }
    })
          .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
        }
    })
          .addOnProgressListener(new OnProgressListener<Uri>() {
      @Override
      public void onProgress(Uri uri) {
          double progress =
          textViewStatus.setText((int) progress + "% Uploading...");
      }
  });
}

0 个答案:

没有答案
相关问题