我正在尝试更新我的用户个人资料(特别是照片),方法是让choosepicker从我的图库中选择图片并将其上传到我的firebase认证存储。它的工作几乎没有:照片上传到我描述的路径。
问题是我不能像我想的那样回复它。我想更新用户个人资料照片并使用UpdateProfileRequest
,但它不会更新任何内容,也无法检索任何内容。
以下是代码:
final ImageView photo = (ImageView) findViewById(R.id.photoURI);
if (photoUri == null) {
photo.setImageResource(R.drawable.user);
Toast.makeText(LoginActivity.this,"DP is Empty",Toast.LENGTH_SHORT).show();
} else {
photo.setImageURI(photoUri);
}
Button editPhoto = (Button) findViewById(R.id.edit_photo);
editPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
});
这是我的活动结果代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
final Uri selectedImageUri = data.getData();
// Get a reference to store file at User_photos/<FILENAME>
StorageReference photoRef = storageReference.child(userID).child(selectedImageUri.getLastPathSegment());
// Upload file to firebase Storage
photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
String imageUrl = downloadUrl.toString();
User user = new User(imageUrl);
databaseReferencenew.child(userID).child("Profile_Pic_URL").setValue(user);
FirebaseUser user2 = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(mName)
.setPhotoUri(selectedImageUri)
.build();
user2.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "User profile updated.");
Toast.makeText(LoginActivity.this,"User profile updated.",Toast.LENGTH_SHORT).show();
}
});
}
});
}
}