我试图将图片上传到firebase,它可以完美地工作直到上传部分。我能够在firebase存储中看到上传的图像。但是,一旦上传图像,无法检索下载URL。
以下是我实施的代码。在以前版本的firebase存储11.8.0上,我能够使用task.getDownloadUrl()
获取URL,但在最新版本中,它失败了。
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
String downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
mUserDatabase.child("image").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
mProgressDialog.dismiss();
Toast.makeText(SettingsActivity.this, "Success", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(SettingsActivity.this, "Error while setting image to database", Toast.LENGTH_LONG).show();
}
}
});
}
});