public class NameVse extends AppCompatActivity {
private ImageView imageprofilregvse;
private EditText nameEditvse;
private Button buttonsavenamevse;
private StorageReference storageprofilpicsRef;
private FirebaseStorage storage;
private Uri imageUri;
private String myUri = "";
private String checker = "";
private StorageTask uploadTask;
private FirebaseAuth mAuth;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name_vse);
imageprofilregvse= (ImageView) findViewById(R.id.imageprofilregvse);
nameEditvse= (EditText) findViewById(R.id.nameEditvse);
buttonsavenamevse = (Button) findViewById(R.id.buttonsavenamevse);
storageprofilpicsRef = FirebaseStorage.getInstance().getReference().child("ProfileImagevse");
storage = FirebaseStorage.getInstance();
mAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference().child("User");
buttonsavenamevse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checker.equals("clicked")) {
ValidateControler();
} else {
}
}
});
imageprofilregvse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checker = "clicked";
CropImage.activity().setAspectRatio(1, 1).start(NameVse.this);
}
});
getUserInfo();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
imageUri = result.getUri();
imageprofilregvse.setImageURI(imageUri);
} else {
Toast.makeText(this, "Произошла ошибка", Toast.LENGTH_SHORT).show();
}
}
private void ValidateControler() {
if (TextUtils.isEmpty(nameEditvse.getText().toString()))
Toast.makeText(this, "Введите ваше имя", Toast.LENGTH_SHORT).show();
else if (checker.equals("clicked")) ;
{
uploadProfilImage();
}
}
private void uploadProfilImage() {
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setTitle("Идет загрузка");
progressDialog.setMessage("Пожалуйста подождите");
progressDialog.show();
if (imageUri !=null){
final StorageReference fileRef= storageprofilpicsRef.child(mAuth.getCurrentUser().getUid()+"jpg");
uploadTask=fileRef.putFile(imageUri);
uploadTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if (!task.isSuccessful()){
throw task.getException();
}
return fileRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task <Uri>task) {
if (task.isSuccessful()){
Uri downloadUri=task.getResult();
myUri=downloadUri.toString();
HashMap<String,Object> userMap=new HashMap<>();
userMap.put("uid",mAuth.getCurrentUser().getUid());
userMap.put("name",nameEditvse.getText().toString());
userMap.put("image",myUri);
databaseReference.child(mAuth.getCurrentUser().getUid()).updateChildren(userMap);
progressDialog.dismiss();
startActivity(new Intent(NameVse.this,HomeActivity.class));
}
}
});
}
else {
Toast.makeText(this, "Изображение не выбрано", Toast.LENGTH_SHORT).show();
}
}
private void getUserInfo() {
databaseReference.child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()&&snapshot.getChildrenCount()>0)
{
String name=snapshot.child("name").getValue().toString();
nameEditvse.setText(name);
if (snapshot.hasChild("image")) {
String image = snapshot.child("image").getValue().toString();
Picasso.get().load(image).into(imageprofilregvse);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
Прописал такую простенькую передачу фото,имени и uid пользователя. Но почему то происходит только передача фото в storage а имя и uid не передаются и real time data base остаётся пустой. Что здесь не так?