Skip to content

Instantly share code, notes, and snippets.

@romrozen
Created August 2, 2017 10:37
Show Gist options
  • Save romrozen/93fde1569ceadfb41d41ee91cf03a4cb to your computer and use it in GitHub Desktop.
Save romrozen/93fde1569ceadfb41d41ee91cf03a4cb to your computer and use it in GitHub Desktop.
private void uploadShotToFirebase() {
final String path = "UserImages/" + UUID.randomUUID() + ".png";
StorageReference imagesRef = mFirebaseStorage.getReference(path);
StorageMetadata imageMetaData = new StorageMetadata.Builder().setCustomMetadata("text2", "some metadata2").build();
//show progress circle, disable upload button
UploadTask uploadTask = imagesRef.putBytes(bitmapInByteArray, imageMetaData);
uploadTask.addOnSuccessListener(getActivity(),new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//enable the button and disable the progress
Uri uri = taskSnapshot.getDownloadUrl();
//// TODO: 16-Jun-17 add a checkbox to confirm with the user if he wants his post to go public
writeNewPost(mAuth.getCurrentUser().getUid(),mAuth.getCurrentUser().getDisplayName(),"title","body",uri.toString(),false);
}
});
}
private void writeNewPost(String userId, String username, String title, String body,String imgUrl,boolean publicPost) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String dbReferencePost = "posts";
//String dbReferencePost = "Coffee-Posts";
String key = mDatabase.child(dbReferencePost).push().getKey();
Post post = new Post(userId, username,imgUrl,publicPost,"EN",key);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
//1childUpdates.put("/Coffee-Posts/" + key, postValues);
childUpdates.put("/posts/" + key, postValues);
//childUpdates.put("/Coffee-user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment