[英]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.