通过uid获取Firebase个人资料照片

时间:2018-08-07 18:23:33

标签: android firebase firebase-storage

我是初学者,请使用此代码帮助我。.它在图像视图中什么都没有显示...我如何获取当前登录用户的个人资料图像。

这是我的活动

*.y

这里是Xml

import com.bumptech.glide.Glide;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;

public class Profile extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private DatabaseReference mDatabaseUsers;
    String post_key = null;
    private StorageReference mStorageRef;
    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs";
    private TextView txt,url;
    private DatabaseReference mDatabase;
private ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile2);

        img=(ImageView)findViewById(R.id.userimg);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
        mStorageRef = FirebaseStorage.getInstance().getReference().child("profile_images");
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        String name = sharedpreferences.getString("name", null);
        editor.commit();
        txt = (TextView) findViewById(R.id.user);
        txt.setText(name);


        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user != null) {
            // Name, email address, and profile photo Url

            Uri photoUrl = user.getPhotoUrl();
            Glide.with(this).load(photoUrl).into(img);
            String uid = user.getUid();
            //Picasso.with(context).load(photoUrl).into(img);

        }
    }


}

这是我的火力基地的样子

<ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="45dp"
            android:id="@+id/userimg"

            />

这是日志

Users
HzId4iQoBiZl4vcC2lcO7kHxFvx1
Username: 
"Humayun"
image: 
"https://firebasestorage.googleapis.com/v0/b/my-..."
name: 
"Being"

我是初学者,请使用此代码帮助我。.它在图像视图中什么都没有显示...如何获取当前登录用户的个人资料图像。

3 个答案:

答案 0 :(得分:0)

如果您使用电子邮件/密码进行身份验证。默认情况下,它不包含profileImage。您必须通过

更新您的个人资料图片(也是另一个属性)
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Humayun")
        .setPhotoUri("https://firebasestorage.googleapis.com/v0/b/my-...")
        .build();

user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User profile updated.");
                }
            }
        });

然后您将能够像以前一样通过用户获取您的个人资料图片

Uri photoUrl = user.getPhotoUrl();
Glide.with(this).load(photoUrl).into(img);

答案 1 :(得分:0)

根据您的数据库结构

  

Users HzId4iQoBiZl4vcC2lcO7kHxFvx1 Username: "Humayun" image: "https://firebasestorage.googleapis.com/v0/b/my-..." name: "Being"

您将需要构造一个链接来访问图像URL。 使用此代码

database.child(user.getUID()).child(user.getDisplayName()).child("image")
.addListenerForSingleValueEvent(new ValueEventListener(){
       //inside the ondatachange, get the url
       //snapshot.getValue().toString() then you can set the image
    });

答案 2 :(得分:0)

这对我有用。

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        mDatabase.child(user.getUid()).child(user.getDisplayName()).child("image")
                .addListenerForSingleValueEvent(new ValueEventListener(){
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        String image=dataSnapshot.getValue().toString();
                        Picasso.with(Profile.this).load(image).into(img);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }

                });