簡體   English   中英

如何使用 Firestore 中的圖像 url 從 firebase 存儲中獲取圖像

[英]How to get image from firebase storage with the image url that is in firestore

我正在嘗試在 glide 的幫助下從 firebase 存儲中獲取我的圖像。我還從 firebase 中獲取用戶名並且它正在顯示。當我直接使用 Z572D4E421E5E6B9BC12D815E 時,我無法在 glide 中使用圖像。因為我首先想將 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>

我的火庫收藏

在此處輸入圖像描述

請注意, .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);

            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM