此处提供的解决方案(How to get URL from Firebase Storage getDownloadURL)是指已弃用的方法。
我尝试了此处提供的其他解决方案,但没有成功。从Android Studio 3.2.1开发的Android应用程序将图像上传到Firebase后,我无法检索正确的下载URL。在Android Studio中运行调试器时,我可以在downloadUrl变量中看到正确的结果,但无法弄清楚如何访问它。如何访问下面的屏幕快照中的zzaa
给出的值并将其存储在变量downloadUrl
中?
下面是调试器的屏幕快照,所需的值在zzaa
上可见。
这是我的代码:
Uri selectedImageUri = data.getData();
//Get a reference to store file at chat_photos/<FILENAME>
StorageReference photoRef =
mChatPhotosStorageReference
.child(selectedImageUri.getLastPathSegment());
UploadTask uploadTask = photoRef.putFile(selectedImageUri);
// Upload file to Firebase Storage
uploadTask
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
Task<Uri> downloadUrl =
taskSnapshot
.getMetadata()
.getReference()
.getDownloadUrl();
// Set the download URL to the message box, so that the user can send it to the database
FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl.toString());
mMessagesDatabaseReference.push().setValue(friendlyMessage);