我正在尝试获取上传到Firebase数据库的图像的下载URL。但是任务Uri imageURL = storageReference.getDownloadUrl();
并未提供存储在Firebase存储器中的图像的实际下载URL,即它给出了-com.google.android.gms.tasks.zzu@27da5837
getdownloadUrl()
在以下情况下不起作用:
Uri imageUrl = storagereference.getdownloadUrl(); //给出错误
请在此处查看代码:
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
final UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
//Task<Uri> imageURL = storageReference.getDownloadUrl();
//Log.i("URL", imageURL.toString()); // gives this url - com.google.android.gms.tasks.zzu@27da5837 which is not correct
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Log.i("Url", String.valueOf(uri));
imageUrl = uri.toString(); //Gives the correct url but it cannot store this uri value outside this function i.e for Intent shown below outside this func.
}
});
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);
}
});
答案 0 :(得分:0)
imageURL是一个任务。您正在尝试打印任务。不是任务的结果。请尝试以下代码:
Task<Uri> imageURL = storageReference.getDownloadUrl();
Log.i("URL", imageURL.toString());
Log.i("URL", imageURL.result.toString());
答案 1 :(得分:0)
调用getDownloadUrl()
将启动异步操作,这可能需要一些时间才能完成。因此,在检索下载URL时,您的主要代码将继续运行,以防止阻止用户。然后,当下载URL可用时,将调用您的onSuccess
。因此,到您的intent.putExtra("imageURL", imageUrl)
现在运行时,imageUrl = uri.toString()
尚未运行。
要确保确实如此,我建议在代码中添加一些日志记录以仅显示流程,或者在调试器中运行它,并在我上面指出的行上设置断点。
要解决此问题,任何需要下载URL的代码都必须位于onSuccess
内,或从那里调用。这不仅适用于此,还适用于所有异步运行的代码,其中包括大多数现代云API。因此,我建议您花一些时间研究这种行为,以使您对以后的工作更加满意。
在您的代码中:
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
final UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
imageUrl = uri.toString();
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);
}
});
}
});
另请参阅: