Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an Upload firebase method with ProgressBar out of the Activity?

I am using Firebase upload and I want to write the uploaded method in a class out of the activity as I want to use it several times and at the same time I want to use ProgressBar that shows the progress and as it is known we can't use findViewById out of activity or Fragment.

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


                    }
                }
like image 971
Mustafa Shahoud Avatar asked Jan 27 '26 21:01

Mustafa Shahoud


1 Answers

You can pass Activity as a parameter instead of Context.

like image 174
Doaa Avatar answered Jan 30 '26 11:01

Doaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!