【问题标题】:How to get image from firebase storage with the image url that is in firestore如何使用firestore中的图像url从firebase存储中获取图像
【发布时间】:2025-12-10 07:45:02
【问题描述】:

我正在尝试在 glide 的帮助下从 firebase 存储中获取我的图像。我还从 firebase 中获取用户名并且它正在显示。当我直接在 glide 中使用 url 时,图像确实是鞋。但是我不能这样做,因为我首先想将 url 分配给一个字符串并在 glide 中使用该字符串。但是当我这样做时,没有图像出现。

我的 java 代码

public class profile extends AppCompatActivity {

    CircleImageView circleImageView;
    TextView fullname;
    FirebaseAuth fAuth;
    FirebaseFirestore fStore;
    String userId;
    BottomNavigationView bottomNavigationView;
   String uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();
        circleImageView = findViewById(R.id.profilepicture2);
        fullname = findViewById(R.id.nameinprofile);
        userId = fAuth.getCurrentUser().getUid();
        bottomNavigationView = findViewById(R.id.bottom_navigation2);
        bottomNavigationView.setSelectedItemId(R.id.profilelogo);  

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
               switch (item.getItemId()){
                case R.id.profilelogo:
                    return true;
                case R.id.home:
                    startActivity(new Intent(getApplicationContext(),homepage.class));
                    return true;
                   case R.id.addlogo:
                       startActivity(new Intent(getApplicationContext(), search.class));
                       return true;

            }
            return false;
        }
        });

        final DocumentReference documentReference = fStore.collection("Userdata").document(userId);
        documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                fullname.setText(documentSnapshot.getString("Full name"));
                 uri = documentSnapshot.getString("imageUrl");
            }
        });
        Glide.with(this).load(uri).into(circleImageView);
    }
    }

我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".profile">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginBottom="383dp"
        android:orientation="vertical"
        android:background="@drawable/borderbottomgrey"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profilepicture2"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:src="@drawable/profile" />

        <TextView
            android:id="@+id/nameinprofile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text=""
            android:textColor="#000000" />
    </LinearLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bordertopgrey"
        app:itemIconTint="#000000"
        app:itemTextColor="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/menu_navigation"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的 Firestore 收藏

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    注意.addSnapshotListener 是异步的(在不同的线程上运行)。 加载图片的代码很可能会在您从 Firebase 获取 URL 之前执行。

    尝试将 Glide 调用放在 onEvent 回调中

    final DocumentReference documentReference = fStore.collection("Userdata").document(userId);
            documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                    fullname.setText(documentSnapshot.getString("Full name"));
                     uri = documentSnapshot.getString("imageUrl");
    
                     Glide.with(YourActivity.this).load(uri).into(circleImageView);
    
                }
            });
    

    【讨论】:

    • 感谢您找到我。我尝试按照您说的做,但没有锻炼,并且图像视图为空白。