在firebase存储中putfile后,OnComlete函数没有调用

时间:2018-06-06 23:23:05

标签: java android firebase firebase-storage

嘿我正在将图片上传到firebase存储。它正在成功上传。为了获取网址,我使用了Firebase文档中的确切代码Here it is 但它没有在addOnCompleteListener中调用onComplete函数 这是我的代码(面向功能问题" _donClicked(查看视图)最后一个功能) P.S这不是活动的完整代码

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

}

2 个答案:

答案 0 :(得分:0)

我有类似你的代码,但我仍然调用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
                  // ...
              }
          }
     });

答案 1 :(得分:0)

所以我找到了解决方案...... 基本上问题是在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();
            }
        }
    });
}

P.S我是android和Java的新手,如果这个问题很愚蠢,请原谅我