如何使用Activity编写带有ProgressBar的Upload firebase方法?

问题描述 投票:1回答:1

我正在使用Firebase上传,我想在活动之外的类中编写上载的方法,因为我想多次使用它,同时我想使用ProgressBar来显示进度,众所周知,无法在活动或片段之外使用findViewById。

           uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                taskSnapshot.getError();
                float progress = (float) ((100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount());
                System.out.println("Upload is " + progress + "% done");
                 **Here I wanna use a progressBar**

            }
        }).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
                System.out.println("Upload is paused");
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                int errorCode = ((StorageException) exception).getErrorCode();
                String errorMessage = exception.getMessage();
                // test the errorCode and errorMessage, and handle accordingly
                Log.d(TAG, "onSuccess: The photo has NOT been uploaded" + errorCode + errorMessage);
                Toast.makeText(mContext, "Sorry the photo has not been uploaded .. Please Try again", Toast.LENGTH_SHORT);
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                circleProgressBar.setVisibility(View.GONE);
                Log.d(TAG, "onSuccess: The photo is being uploaded");
                Toast.makeText(mContext, "The photo has been uploaded successfully", Toast.LENGTH_SHORT).show();
                //
            }
        }).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 ref.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    if (downloadUri != null){
                        String photoStringUrl = downloadUri.toString();
                        Log.d(TAG, "onComplete: Upload " + photoStringUrl);

                        //Inserting the profile photo into database {user_profile_account}
                        firebaseUtilities.saveProfilePhotoToDatabase(photoStringUrl);


                    }
                }
java android firebase firebase-storage
1个回答
1
投票

您可以将Activity而不是Context作为参数传递。

© www.soinside.com 2019 - 2024. All rights reserved.