en este ejemplo se ve como muestra el nombre y la foto asociada a la cuenta de un usuario, también como modificar la foto del usuario, pero no como actualizar el nombre del usuario por uno nuevo
public class Main3Activity extends AppCompatActivity {
private TextView txtusu;
private Button btnatras;
private ImageView imgperfil;
private ProgressDialog progressDialog;
private DatabaseReference mDataBase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private int CAMERA_REQUEST_CODE = 1;
private StorageReference mStorage;
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mAuth = FirebaseAuth.getInstance();
progressDialog= new ProgressDialog(this);
txtusu= (TextView) findViewById(R.id.txtusu);
btnatras= (Button) findViewById(R.id.btnatras);
imgperfil= (ImageView) findViewById(R.id.imgperfil);
mStorage=FirebaseStorage.getInstance().getReference();
imgperfil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
if (intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(Intent.createChooser(intent, "Seleccione una imagen de la galería"), CAMERA_REQUEST_CODE);
}
}
});
btnatras = (Button) findViewById(R.id.btnatras);
btnatras.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAuth.getCurrentUser() != null)
mAuth.signOut();
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null){
mStorage= FirebaseStorage.getInstance().getReference();
mDataBase = FirebaseDatabase.getInstance().getReference().child("Alumnos");
mDataBase.child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
txtusu.setText(String.valueOf(dataSnapshot.child("Usuario").getValue()));
String imageUrl=String.valueOf(dataSnapshot.child("Foto_Perfil").getValue());
if (URLUtil.isValidUrl(imageUrl)){
Picasso.with(Main3Activity.this).load(Uri.parse(imageUrl)).into(imgperfil);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}else {
startActivity(new Intent(Main3Activity.this, Main2Activity.class));
finish();
}
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
if (mAuth.getCurrentUser() == null) {
return;
}
progressDialog.setMessage("Cargando imagen...");
progressDialog.show();
final Uri uri = data.getData();
if (uri == null) {
progressDialog.dismiss();
return;
}
if (mAuth.getCurrentUser() == null)
return;
if (mStorage == null)
mStorage = FirebaseStorage.getInstance().getReference();
if (mDataBase == null)
mDataBase = FirebaseDatabase.getInstance().getReference().child("Alumnos");
final StorageReference filepath = mStorage.child("Foto_Perfil").child(uri.getLastPathSegment());
final DatabaseReference currentUserDB = mDataBase.child(mAuth.getCurrentUser().getUid());
currentUserDB.child("Foto_Perfil").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String image = dataSnapshot.getValue().toString();
if (!image.equals("default") && !image.isEmpty()) {
Task<Void> task = FirebaseStorage.getInstance().getReferenceFromUrl(image).delete();
task.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Main3Activity.this, "Imagen borrada", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main3Activity.this, "Error, Imagen no borrada", Toast.LENGTH_SHORT).show();
}
}
});
}
currentUserDB.child("Foto_Perfil").removeEventListener(this);
filepath.putFile(uri).addOnSuccessListener(Main3Activity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Uri downloadUri = taskSnapshot.getDownloadUrl();
Toast.makeText(Main3Activity.this, "Finalizado", Toast.LENGTH_SHORT).show();
Picasso.with(Main3Activity.this).load(downloadUri).fit().centerCrop().into(imgperfil);
final DatabaseReference currentUserDB = mDataBase.child(mAuth.getCurrentUser().getUid());
currentUserDB.child("Foto_Perfil").setValue(downloadUri.toString());
}
}).addOnFailureListener(Main3Activity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(Main3Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}