从Firebase检索用户个人资料图像,但未在ImageView中显示

时间:2018-08-03 19:44:44

标签: android firebase firebase-authentication

我正在使用此代码来检索图像,但未在ImageView中显示。我是初学者。

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

    Uri photoUrl = user.getPhotoUrl();
    img.setImageURI(photoUrl);
    // The user's ID, unique to the Firebase project. Do NOT use this value to
    // authenticate with your backend server, if you have one. Use
    // FirebaseUser.getToken() instead.
    String uid = user.getUid();
}

2 个答案:

答案 0 :(得分:0)

由于您正在从getPhotoUrl()实例访问FirebaseAuth,这意味着您正在尝试从远程位置(从Web)获取图像。

ImageView不支持从远程位置加载图像。使用ImageView只能加载本地文件系统上的资源或文件。

因此,您可以自己下载图像,然后为setImageURI指定本地路径,或者使用一些最佳的图像加载或缓存库-

GlidePicasso

这些图书馆中的任何一个都可以使您的生活更轻松。

答案 1 :(得分:0)

Java活动

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);

        }
    }


}

.XML

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

                />

Firebase

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