【问题标题】:How to write an Upload firebase method with ProgressBar out of the Activity?如何在 Activity 之外使用 ProgressBar 编写 Upload firebase 方法?
【发布时间】:2019-02-27 21:12:46
【问题描述】:

我正在使用 Firebase 上传,我想在 Activity 之外的一个类中编写上传的方法,因为我想多次使用它,同时我想使用 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);


                    }
                }

【问题讨论】:

  • 写一个有进度条的片段,用几次。
  • 实际上,我刚刚发现最好的解决方案是将 Activity 作为参数而不是 Context 传递,这样我们就可以调用 activity.findViewById(),然后在实例化类时使用 ActivityName。这个。

标签: java android firebase firebase-storage


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2014-03-13
    • 2021-06-11
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多