通过Firebase身份验证添加图像和其他数据

时间:2018-11-03 07:39:14

标签: firebase android-studio firebase-authentication

我想将所有用户信息存储到firebase中,并将电子邮件和密码传递给firebase auth,然后将图像插入firebase中,但是在这里我在taskSnapshot.getDownloadUrl()。toString())遇到了错误。 而且我也不确定这是否是将用户电子邮件和密码传递给firebase auth的正确方法

public void AddUser(最终字符串UserEmail,最终字符串用户名,最终字符串密码,                         最终字符串PhoneNumber,最终字符串ConfirmPassword,最终字符串地址){

    //first we encode the email into "," to enable check the firebase database
    String email = UserEmail.replace(".", ",");


    Userdatabase = FirebaseDatabase.getInstance().getReference("User").child(email);

    Log.d("UserEmail", Userdatabase.toString());

    Userdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                String value = dataSnapshot.getValue(String.class);

                Log.i(TAG, "UserEmail : " + value + " Had Already Exist");

                Toasty.warning(getApplicationContext(), "The Email you use already Exist !", Toast.LENGTH_SHORT, true).show();
                return;
            }


            if (!dataSnapshot.exists()) {
                if (imageUri != null) {
                    StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                            + "." + getFileExtension(imageUri));

                    mUploadTask = fileReference.putFile(imageUri)
                            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                    Handler handler = new Handler();
                                    handler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                        }
                                    }, 500);

                                    Toast.makeText(Signup.this, "Register successful", Toast.LENGTH_LONG).show();

                                    final User user = new User(Address, confirmPassword, UserEmail, Password, PhoneNumber, Username,
                                            taskSnapshot.getDownloadUrl().toString());
                                    Userdatabase.setValue(user);

                                }
                            });
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            return;
        }
    });

    firebaseAuth.createUserWithEmailAndPassword(UserEmail, Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            progressDialog.dismiss();

            if (!task.isSuccessful()) {
                Log.i(TAG, "Buyer FirebaseAuth Register : Fail");

                Toasty.error(getApplicationContext(), "The Email you use already Exist !", Toast.LENGTH_SHORT, true).show();

                } else {
                Log.i(TAG, "Buyer FirebaseAuth Register : Success");

                UserEmail.replace(".", ",");

                final User user = new User(Address, confirmPassword, UserEmail, Password, PhoneNumber, Username);

                Userdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (!dataSnapshot.exists()) {
                            Userdatabase.setValue(user);
                            Log.i(TAG, "FirebaseDatabase Add Buyer : Success");
                            Toasty.success(getApplicationContext(), "Register Complete", Toast.LENGTH_SHORT, true).show();
                            }
                            }
                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                        Log.w(TAG, "Database Error");
                        }
                        });
                }
                }
                });
}

}

1 个答案:

答案 0 :(得分:1)

这是问题taskSnapshot.getDownloadUrl().toString()

最新的Firebase库通过调用url中的上传参考来提供下载asychronous task

这是完整的代码

私有字符串链接;

mUploadTask = fileReference.putFile(imageUri)
              .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                            @Override
                                            public void onSuccess(Uri downloadUri) {
                                                link = downloadUri.toString;
                                                Toast.makeText(Signup.this, "Register successful", Toast.LENGTH_LONG).show();
                                                final User user = new User(Address, confirmPassword, UserEmail, Password, PhoneNumber, Username,
                                                        link));
                                                Userdatabase.setValue(user);
                                            }
                                        });
                            }
                        });