上传用户Firebase的个人资料图片 [英] Upload profile image for a user Firebase
问题描述
我正在尝试将图像添加到android的实时数据库(firebase)中的用户信息中.我已将图像上传到Firebase存储器上,但是如何为该用户在数据库中添加图像?
I am trying to add an image to the user information in the real time database(firebase) for android. I have uploaded the image on the firebase storage but how will I be able to add the image in the database for that user?
以下代码:
//inside onCreate() method
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i,request_code);
}
});
我在这里单击图像视图,因此可以对其进行更改并从图库中获取图像.
Here I am clicking on the imageview, so I will be able to change it and get an image from the gallery.
在这里,我对用户进行身份验证并将数据发送到数据库:
Here I authenticate the user and send data to the database:
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(StudentSignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(getApplicationContext(), "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(StudentSignUpActivity.this, HomeActivity.class));
finish();
}
}
});
mCurrentUser=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference newStudent=mDatabase.push();
newStudent.child("email").setValue(email);
newStudent.child("password").setValue(password);
newStudent.child("name").setValue(name);
newStudent.child("date").setValue(dates);
newStudent.child("phone").setValue(number);
newStudent.child("uid").setValue(mCurrentUser.getUid());
//outside of onCreate()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==request_code&&resultCode==RESULT_OK){
Uri uri=data.getData();
StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
}
});
}
}
在上面的代码中,我已将图像上传到Firebase存储.现在,我将如何为特定用户将该图像添加为子图像.
In the above code I have uploaded the image to the firebase storage. Now how will i be able to add that image as a child for a specific user.
我认为我需要做这样的事情:
I think I need to do something like this:
newStudent.child("image").setValue(uri_here);
但是我无法弄清楚如何获取图像的uri,以及如何在setValue()
中添加该uri,因为它是另一种方法.
But I am unable to figure how to get the uri of the image and how to add that uri in the setValue()
since its in another method.
推荐答案
您可以在成功侦听器中使用方法getDownloadUrl()
来访问下载URL:
You can use the method getDownloadUrl()
in the success listener to access the download URL:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==request_code&&resultCode==RESULT_OK){
Uri uri=data.getData();
StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
newStudent.child("image").setValue(downloadUrl);
}
});
}
}
顺便说一句,建议不要使用push()
,而要使用uid
作为键来存储用户数据.这将使您的数据更易于查找.
As an aside, instead of using push()
, I recommend storing the user's data with the uid
as the key. This will make your data easier to find.
private DatabaseReference newStudent;
mCurrentUser=FirebaseAuth.getInstance().getCurrentUser();
newStudent=mDatabase.child(mCurrentUser.getUid());
newStudent.child("email").setValue(email);
// etc
这篇关于上传用户Firebase的个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!