简体   繁体   中英

Having error while uploading user profile image to firebase, during authentication in android

截屏

截图

i developed authentication with firebase it worked fine but i want to add an extra module to add user profie image. so I wrote 2 functions first for the user signup with email and password in firebase realtime database, and the second for uploading user profile image along with it... i just add pimage variable in user model class, but i am getting errors here in previous parameters of user model object and (uri.toString());

here is the code for creating user with email and password

private void register_doctor () {
            String dname = et_name.getText().toString().trim();
            String demail = et_email.getText().toString().trim();
            String dpass = et_pass.getText().toString().trim();
            String dcpass = et_cnf_pass.getText().toString().trim();
            String dcontact = et_contact.getText().toString().trim();
            String dcity = et_city.getText().toString().trim();
            String dage = et_age.getText().toString().trim();


            if (dname.isEmpty()) {
                et_name.setError("Full Name is Required");
                et_name.requestFocus();
                return;
            }
            if (demail.isEmpty()) {
                et_email.setError("Email is Required");
                et_email.requestFocus();
                return;
            }

            if (dpass.isEmpty()) {
                et_pass.setError("Password is Required");
                et_pass.requestFocus();
                return;
            }

            if (dpass.length() < 6) {
                et_pass.setError("Password Length Should be greater than 6 characters");
                et_pass.requestFocus();
                return;
            }

            if (dcpass.isEmpty()) {
                et_cnf_pass.setError("Password is Required");
                et_cnf_pass.requestFocus();
                return;
            }
            if (!dcpass.equals(dpass)) {
                et_cnf_pass.setError("Password Does not Matched!");
                et_cnf_pass.requestFocus();
                return;
            }

            if (dcontact.isEmpty()) {
                et_contact.setError("Contact is Required");
                et_contact.requestFocus();
                return;
            }

            if (dcity.isEmpty()) {
                et_city.setError("City is Required");
                et_city.requestFocus();
                return;
            }

            if (dage.isEmpty()) {
                et_age.setError("Age is Required");
                et_age.requestFocus();
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            mAuth.createUserWithEmailAndPassword(demail, dpass)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                User user = new User(dname, demail, dcontact, dcity, dage);👈

                                FirebaseDatabase.getInstance().getReference("doctors")
                                        .child("Doctors_Registration")
//                                        .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                        .child(demail.replace(".", ","))
                                        .setValue(user)
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {

                                                if (task.isSuccessful()) {
                                                    Toast.makeText(signup.this, "Doctor Registered", Toast.LENGTH_SHORT).show();
                                                    progressBar.setVisibility(View.GONE);
                                                } else {
                                                    Toast.makeText(signup.this, "Failed to Registered, Try Again!" + task.getException(), Toast.LENGTH_LONG).show();
                                                    progressBar.setVisibility(View.GONE);
                                                }
                                            }
                                        });
                            } else {
                                Toast.makeText(signup.this, "Failed to Registered, Try Again!", Toast.LENGTH_LONG).show();
                                progressBar.setVisibility(View.GONE);
                            }
                        }
                    });

        }

here is the function to upload user profile image

 private void upload_image() {

        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference uploader = storage.getReference("Image1"+ new Random().nextInt(50));
        uploader.putFile(filepath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        uploader.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                FirebaseDatabase db = FirebaseDatabase.getInstance();
                                DatabaseReference root = db.getReference("Doctors_Registration");
                                User obj = new User(uri.toString()); 👈
                                root.child(et_email.getText().toString()).setValue(obj);
//                                iv.setImageResource(R.drawable.ic_launcher_background);
                                Toast.makeText(signup.this, "Profile Image Uploaded", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

    }

here is the user model class code

public class User {
    public String name, email, password, contact, city, age, pimage;
    public  User(){
    }
    public User(String name, String email, String contact, String city, String age, String pimage){
        this.name = name;
        this.email = email;
        this.contact = contact;
        this.city = city;
        this.age = age;
        this.pimage = pimage;
    }
}

Your User class has two constructors:

  1. One that takes no arguments.
  2. And one that takes 6 string values as its arguments.

In the two screenshots you are trying to construct a User object:

  1. First with a single string value,
  2. And then with 5 string values.

Since neither of these calls matches the constructors you defined, the compiler is unable to compile your code.

To allow constructing a User , makes sure you either add constructor overloads for the parameters you want to pass, or pass the parameters that match with the constructors you defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM