How To Get Image Downloadable Url From Firebase Storage And Store In Firebase Database?
I am storing users profile pictures in firebase storage. I want to get the downloadable URL of those profile pictures and store each users profile picture URL under his/her User ID
Solution 1:
1> Create a map for some information...
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String email = user.getEmail();
String name = user.getDisplayName();
String userName = "USER_NAME"; // set userName here...String id = user.getUid();
Map<String,String> imageInfo = newHashMap<>();
imageInfo.put("email", email);
imageInfo.put("id", id);
imageInfo.put("name", name);
imageInfo.put("userName", userName);
2> After uploading file , add information to database..
// inside OnSuccessListenerDatabaseReferencedb= FirebaseDatabase.getInstance().getReference("Users");
db.push().setValue(imageInfo);
// this will add all information to your database...
3> Downloading image with file name...
Get file name from database using database query... And use these codes to download image and set to any imageView...
try {
finalFiletmpFile= File.createTempFile("img", "png");
StorageReferencereference= FirebaseStorage.getInstance().getReference("Photos");
// "id" is name of the image file....
reference.child(id).getFile(tmpFile).addOnSuccessListener(newOnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@OverridepublicvoidonSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmapimage= BitmapFactory.decodeFile(tmpFile.getAbsolutePath());
userImageView.setImageBitmap(image);
}
});
} catch (Exception e) {
e.printStackTrace();
}
Post a Comment for "How To Get Image Downloadable Url From Firebase Storage And Store In Firebase Database?"