如果您有类似这样的课程
,是否可能type
从Android首先上传图片,此图片保存在firebase存储中。 在下一步中,您将使用上次上传的图像发布您的姓名,年龄和存储空间,并将此图像与姓名和年龄相关联
例如
姓名:'John'
年龄:50
url:firebase url image
答案 0 :(得分:1)
创建模型类
public class Model implements Serializable {
public String name;
public String age;
public String photoUrl;
public Model (){
}
public Model (String name, String age,String photoUrl){
this.name= name;
this.age= age;
this.photoUrl= photoUrl;
}
}
在firebase存储中插入图片,
UploadTask uploadTask;
uploadTask = imagePathReference.putBytes(dataNew);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e("firebase ", " addOnFailureListener ");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//taskSnapShot u will get the download url.
}
});
保存下载网址,然后在获取姓名和年龄后的下一步
Model user = new Model (name,age,url);
此对象(用户)中将包含所有数据。现在将对象保存在实时firebase表中。
答案 1 :(得分:1)
是。您只需使用Firebase Storage上传图片,在回调中即可获得该图片的可下载网址。然后将所有值放在自定义对象中,并将该对象保存在Firebase的RealTime数据库中。
答案 2 :(得分:0)
我已经使用过这种类型的任务
根据您的要求尝试此代码,
StorageReference riversRef = mStorageRef.child("posts/images" + userId + "/" + String.valueOf(System.currentTimeMillis()) + ".jpg");
riversRef.putBytes(bytes)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.d("downloadUrl", "" + downloadUrl);
writeNewPost(userId, user.getUsername(), user.getProfilePic(), String.valueOf(downloadUrl), body);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
}
});
和 writeNewPost 功能如下 -
private void writeNewPost(String userId, String username, String userImage, String img, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child(KeyTAG.TAG_POSTS).push().getKey();
Post post = new Post(userId, username, body, img, userImage, key, DateTimeUtil.GetUTCdatetimeAsDate(),"image","noThumb");
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/" + KeyTAG.TAG_POSTS + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
mDialog.hide();
}