上传用户Firebase的个人资料图片

时间:2017-10-17 21:25:18

标签: android image firebase firebase-realtime-database

我正在尝试将图像添加到Android的实时数据库(firebase)中的用户信息中。我已将图像上传到firebase存储,但我如何能够为该用户在数据库中添加图像?

以下代码:

//inside onCreate() method

img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(Intent.ACTION_PICK);
            i.setType("image/*");
            startActivityForResult(i,request_code);
        }
    });

在这里,我点击了imageview,所以我可以更改它并从图库中获取图像。

在这里,我对用户进行身份验证并将数据发送到数据库:

 auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(StudentSignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toast.makeText(getApplicationContext(), "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Toast.makeText(getApplicationContext(), "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                startActivity(new Intent(StudentSignUpActivity.this, HomeActivity.class));
                                finish();
                            }
                        }
                    });

mCurrentUser=FirebaseAuth.getInstance().getCurrentUser();
            DatabaseReference newStudent=mDatabase.push();
            newStudent.child("email").setValue(email);
            newStudent.child("password").setValue(password);
            newStudent.child("name").setValue(name);
            newStudent.child("date").setValue(dates);
            newStudent.child("phone").setValue(number);
            newStudent.child("uid").setValue(mCurrentUser.getUid());


//outside of onCreate()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==request_code&&resultCode==RESULT_OK){
        Uri uri=data.getData();
        StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            }
        });
    }
}

在上面的代码中,我已将图像上传到firebase存储。现在,我将如何将该图像作为特定用户的子图像添加。

我想我需要做这样的事情:

 newStudent.child("image").setValue(uri_here);

但我无法弄清楚如何获取图像的uri以及如何在setValue()中添加uri,因为它在另一种方法中。

2 个答案:

答案 0 :(得分:1)

您可以使用成功侦听器中的方法getDownloadUrl()来访问下载URL:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==request_code&&resultCode==RESULT_OK){
        Uri uri=data.getData();
        StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                newStudent.child("image").setValue(downloadUrl);
            }
        });
    }
}

另外,我建议不要使用push(),而是以uid为键存储用户数据。这将使您的数据更容易找到。

private DatabaseReference newStudent;

mCurrentUser=FirebaseAuth.getInstance().getCurrentUser();
            newStudent=mDatabase.child(mCurrentUser.getUid());
            newStudent.child("email").setValue(email);
            // etc

答案 1 :(得分:0)

仅由于我花了一些时间来找到此答案而更新,getDownloadUrl()不再是taskSnapshot的功能。因此,为了从Firebase Storage获取图像URL,您需要向其中添加一个侦听器 taskSnapshot.getMetadata().getReference().getDownloadUrl()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==request_code&&resultCode==RESULT_OK){
        Uri uri=data.getData();
        StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                taskSnapshot.getMetadata().getReference().getDownloadUrl()
                    .addOnSuccessListener(new OnSuccessListener<Uri>() {

                    @Override
                    public void onSuccess(Uri uri) {
                        newStudent.child("image").setValue(uri);

                    }
                });
            }
        });
    }
}

现在可以安全地将 uri 用于所需的任何内容

相关问题