簡體   English   中英

在Firebase存儲中的putfile之后,OnComlete函數未調用

[英]OnComlete function is not calling after putfile in firebase storage

嘿,我正在將圖片上傳到Firebase存儲。它已成功上傳。 並獲得網址我用從火力地堡文檔確切的代碼 ,但他並沒有叫的onComplete在addOnCompleteListener funtion這里是我的代碼(面向函數問題“_donClicked(查看視圖)最后一個函數)PS這不是活動的完整代碼

public class AddDetails extends AppCompatActivity {
DatePicker datePicker;
static int year;
static int day;
static int month;
static String UId;
static public Uri downloadurl;
Calendar calendar;
Button date_of_birth;
static public String Fname;
static public String Lname;
DatePickerDialog datePickerDialog;
int imagePickerCode=113;
StorageReference firebaseStorage;
Uri imageURI;

public void AddImage(View view)
{
    Intent intent=new Intent(Intent.ACTION_PICK, 
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent,imagePickerCode);
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
        if(requestCode==imagePickerCode)
        {
            if(resultCode==RESULT_OK)
            {
                imageURI=data.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
                    ImageView imageView=(ImageView) findViewById(R.id.displayPic);
                    imageView.setImageBitmap(bitmap);
                    Button button=(Button) findViewById(R.id.addImage);
                    button.setVisibility(View.INVISIBLE);
                    imageView.setVisibility(View.VISIBLE);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        }
}
public void _doneClicked(View view)
{
    firebaseStorage.child("Display Pictures").child(UId).putFile(imageURI)
        .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
            else
            return firebaseStorage.child("Display Pictures").child(UId).getDownloadUrl();
        }
    })
            .addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(Task<Uri> task) {
            if (task.isSuccessful()) {
                downloadurl = task.getResult();
                Log.i("Done",downloadurl.toString());
            }
        }
    });
    Fname=((EditText)findViewById(R.id.firstName)).getText().toString();
    Lname=((EditText)findViewById(R.id.lastName)).getText().toString();
    setResult(RESULT_OK);
    finish();
}

}

我有類似您的代碼,但是我仍然調用onComplete函數。

我認為問題出在您的continueWithTask中,在此行上:

return firebaseStorage.child("Display Pictures").child(UId).getDownloadUrl();

當您這樣做時,firebaseStorage.child()將創建StorageReference的新實例。 也許是這種意外結果的原因。

嘗試遵循文檔示例:

final StorageReference ref = firebaseStorage.child("Display Pictures").child(UId);
UploadTask uploadTask = ref.putFile(imageURI)

Task<Uri> urlTask = uploadTask
      .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();
              } else {
                  // Handle failures
                  // ...
              }
          }
     });

因此,我找到了解決方案...基本上,問題是在addOncompleteListener類中重寫OnComplete函數,該類是在任務完成時運行的“偵聽器”。 在我的函數“ public void _doneClicked(View view)”中,活動可以在調用oncomplete函數之前完成。 所以我將代碼更改為

   public void _doneClicked(View view)
{
    final StorageReference sss=firebaseStorage.child("Display Pictures").child(UId);
    UploadTask up=sss.putFile(imageURI);
    Task<Uri> task=up.
            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
            else
            return sss.getDownloadUrl();
        }
    })
            .addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(Task<Uri> task) {
            if (task.isSuccessful()) {
                downloadurl = task.getResult();
                Log.i("Done",downloadurl.toString());
                Fname=((EditText)findViewById(R.id.firstName)).getText().toString();
                Lname=((EditText)findViewById(R.id.lastName)).getText().toString();
                setResult(RESULT_OK);
                finish();
            }
        }
    });
}

PS我對Android和Java都是新手,如果這個問題愚蠢,請原諒我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM