Reputation: 429
Is it possible if you have class for example like this
public class UserInfo {
String name;
String age;
String url;
}
From android you first upload image and this image is saved in firebase storage. In the next step you publish your name, age and from storage used last uploaded image and linked this image with name and age
For example
name: 'John'
age: 50
url: firebase url image
Upvotes: 3
Views: 5020
Reputation: 1238
I already used this type of task
try this code according to your requirenments,
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
}
});
and the writeNewPost function is as follow -
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();
}
Upvotes: 0
Reputation: 692
Create Model class,
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;
}
}
Insert image in firebase storage,
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.
}
});
save the download url, Then in next step after getting name and age
Model user = new Model (name,age,url);
Here in this object(user) you will have the all the data.Now save the object in realtime firebase table.
Upvotes: 1
Reputation: 1344
Yes. You just simply use Firebase Storage for uploading image, in callback you get a downloadable URL of that image. Then put all the values in the custom object and save that object in RealTime Database of Firebase.
Upvotes: 1