【发布时间】:2018-05-24 02:41:47
【问题描述】:
我正在尝试使用以下结构将数据保存到 firebase
root
--clothing
----clothingImgDownloadUrl
------title
------category
据我了解,我需要引用数据库中要保存新数据的路径。
FirebaseApp app = FirebaseApp.getInstance();
assert app != null;
mDatabase = FirebaseDatabase.getInstance(app);
mClothingRef = mDatabase.getReference("clothing");
我想使用上面创建的服装参考添加新服装
mClothingRef.child(clothingImgDownloadUrl).setValue(clothing);
但是,上面的代码仅在服装图像上传到 firebase 存储后才会运行,并且其下载 url 会返回到应用程序,在应用程序中它可以用作 firebase 数据库中条目的唯一标识符。以下是将图片上传到 Firebase 存储的代码。
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference(FIREBASE_STORAGE_CLOTHING_REFERENCE);
StorageReference photoRef = storageRef.child(mSelectedImageUri.getLastPathSegment());
photoRef.putFile(mSelectedImageUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
Log.d("ADMIN_ACTIVITY", "Called");
Uri downloadUrl = taskSnapshot.getDownloadUrl();
assert downloadUrl != null;
String imageUrl = downloadUrl.toString();
Clothing clothing = new Clothing(imageUrl, title, mCategory);
mClothingRepository.addClothing(clothing);
}
});
当应用程序运行时,我从应用程序的管理部分添加了一件衣服,图像确实被添加到了 firebase 存储中,但是最后一个代码 sn-p 中的 Log 调用没有被调用。当然,如果添加了图像,则应该调用 onSuccess 侦听器,然后运行 addClothing 方法。
为什么没有调用onSuccess 侦听器,我是否正确保存到实时数据库?
【问题讨论】:
标签: java android firebase firebase-realtime-database firebase-storage