0
我想添加一個圖像到Android的實時數據庫(firebase)中的用戶信息。我已經將圖片上傳到firebase存儲中,但我將如何將該圖片添加到該用戶的數據庫中?上傳用戶個人資料圖片Firebase
下面的代碼:
//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);
}
});
在這裏,我點擊ImageView的,所以我將能夠從畫廊改變它,得到的圖像。
在這裏,我認證用戶併發送數據到數據庫中:
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) {
}
});
}
}
在上面的代碼中,我已上傳的圖像到火力存儲。現在,我將如何將該圖像添加爲特定用戶的孩子。
我想我需要做這樣的事情:
newStudent.child("image").setValue(uri_here);
但我無法弄清楚如何獲取圖像的URI,以及如何添加在setValue()
該URI,因爲它在另一種方法。
其實'newStudent = mDatabase.child(mCurrentUser.getUid())'不起作用其返回零點例外,因爲沒有在尚未簽署用戶。上面的代碼用於註冊 –