如何从Firebase存储中获取图像可下载的URL并存储在Firebase数据库中? [英] how to get image downloadable url from firebase storage and store in firebase database?
问题描述
我将用户个人资料图片存储在Firebase存储中.我想获取这些个人资料图片的可下载URL,并将每个用户的个人资料图片URL存储在firebase数据库中他/她的用户ID下. 这是我将图片存储在Firebase存储中的代码.但是我不知道如何获取URL并将其存储在相应用户ID下的数据库中.我还附加了Firebase数据库的快照. Firebase数据库映像
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 in firebase database. Here is my code for storing the picture in firebase storage. But i don't know how to get the URL and store in database under respective user's ID. I have also attached the snapshot of my firebase database. Firebase database image
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
{
progressDialog.setMessage("Uploading...");
progressDialog.show();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userID = user.getUid();
Uri uri = data.getData();
ivProfilePicture.setImageURI(uri);
StorageReference filepath =
storageReference.child("Photos").child(userID);
filepath.putFile(uri).addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(EditProfile.this,"Upload
Done",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
推荐答案
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 = new HashMap<>();
imageInfo.put("email", email);
imageInfo.put("id", id);
imageInfo.put("name", name);
imageInfo.put("userName", userName);
2>上传文件后,将信息添加到数据库.
2> After uploading file , add information to database..
// inside OnSuccessListener
DatabaseReference db = FirebaseDatabase.getInstance().getReference("Users");
db.push().setValue(imageInfo);
// this will add all information to your database...
3>正在下载具有文件名的图像...
3> Downloading image with file name...
使用数据库查询从数据库获取文件名... 并使用这些代码下载图像并设置为任何imageView ...
Get file name from database using database query... And use these codes to download image and set to any imageView...
try {
final File tmpFile = File.createTempFile("img", "png");
StorageReference reference = FirebaseStorage.getInstance().getReference("Photos");
// "id" is name of the image file....
reference.child(id).getFile(tmpFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap image = BitmapFactory.decodeFile(tmpFile.getAbsolutePath());
userImageView.setImageBitmap(image);
}
});
} catch (Exception e) {
e.printStackTrace();
}
这篇关于如何从Firebase存储中获取图像可下载的URL并存储在Firebase数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!