【发布时间】:2018-11-17 00:12:21
【问题描述】:
我正在努力将第二个 url 添加到数据库中。我尝试了其他几种方法并没有走远。我试过单独上传每个,但后来它创建了两个孩子。我最终遵循了Upload Multiple Images on Firebase - Android Studio 的答案,我就在这里。
这是我想要实现的目标:
posts
-LRTx-3WJNP2zxifv7h9
city:
contact_email:
country:
description:
image: URL IMAGE IN STORAGE
image1: URL IMAGE1 IN STORAGE
post_id:
price:
state_province:
title:
user_id:
两张图片都已成功上传到存储,但我的数据库中只有一个网址,如下所示: image1 不存在。
posts
-LRTx-3WJNP2zxifv7h9
city:
contact_email:
country:
description:
image: URL IMAGE IN STORAGE
post_id:
price:
state_province:
title:
user_id:
代码:
if( mSelectedUri !=null ){
Toast.makeText(getActivity(), "uploading image", Toast.LENGTH_SHORT).show();
final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();
StorageReference filepath = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid()+
"/" + postId + "/post_image").child(mSelectedUri.getLastPathSegment());
StorageReference filepath1 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image1").child(mSelectedUri1.getLastPathSegment());
filepath.putFile(mSelectedUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri firebaseUri = taskSnapshot.getDownloadUrl();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Post post = new Post();
post.setImage(firebaseUri.toString());
// post.setImage1(firebaseUri.toString());
post.setCity(mCity.getText().toString());
post.setContact_email(mContactEmail.getText().toString());
post.setCountry(mCountry.getText().toString());
post.setDescription(mDescription.getText().toString());
post.setPost_id(postId);
post.setPrice(mPrice.getText().toString());
post.setState_province(mStateProvince.getText().toString());
post.setTitle(mTitle.getText().toString());
post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child(getString(R.string.node_posts))
.child(postId)
.setValue(post);
}
});
filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri firebaseUri = taskSnapshot.getDownloadUrl();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Post post = new Post();
post.setImage1(firebaseUri.toString());
reference.child(getString(R.string.node_posts))
.child(postId)
.setValue(post);
// resetFields();
}
});
};
【问题讨论】:
标签: android firebase firebase-realtime-database firebase-storage