如何使用Firestore中的图像URL从Firebase存储中获取图像

时间:2020-06-05 13:09:11

标签: java android firebase google-cloud-firestore

我试图借助glide从Firebase存储器中获取图像,也从Firebase中获取用户名并显示出来,当我直接在glide中使用url时,图像确实不存在。我不能这样做,因为我首先想将URL分配给String并在glide内部使用String。但是当我这样做时,没有图像出现。

我的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收藏

enter image description here

1 个答案:

答案 0 :(得分: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);

            }
        });