如何通过在Firestore中的图像URL从Firebase存储获取图像

huangapple go评论78阅读模式
英文:

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

问题

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 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>
英文:

I am trying to get my image from the firebase storage with the help of glide.I am also getting the user's name from the firebase and it is showing up.When I directly use the url in glide the image does shoe.But I cannot do that as I first want to assign the url to a String and use the String inside glide.But when I do that no image shows up.

My java code

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(&quot;Userdata&quot;).document(userId);
documentReference.addSnapshotListener(this, new EventListener&lt;DocumentSnapshot&gt;() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
fullname.setText(documentSnapshot.getString(&quot;Full name&quot;));
uri = documentSnapshot.getString(&quot;imageUrl&quot;);
}
});
Glide.with(this).load(uri).into(circleImageView);
}
}

My xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.profile&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;150dp&quot;
android:layout_marginBottom=&quot;383dp&quot;
android:orientation=&quot;vertical&quot;
android:background=&quot;@drawable/borderbottomgrey&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;de.hdodenhof.circleimageview.CircleImageView
android:id=&quot;@+id/profilepicture2&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;30dp&quot;
android:src=&quot;@drawable/profile&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/nameinprofile&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;10dp&quot;
android:text=&quot;&quot;
android:textColor=&quot;#000000&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;com.google.android.material.bottomnavigation.BottomNavigationView
android:id=&quot;@+id/bottom_navigation2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:background=&quot;@drawable/bordertopgrey&quot;
app:itemIconTint=&quot;#000000&quot;
app:itemTextColor=&quot;#000000&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:menu=&quot;@menu/menu_navigation&quot;
tools:ignore=&quot;MissingConstraints&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

My firestore collection

如何通过在Firestore中的图像URL从Firebase存储获取图像

答案1

得分: 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);
    }
});
英文:

Note that .addSnapshotListener in asynchronous (runs on a different thread).
The code that loads the image most probably gets executed before you get the URL from Firebase.

Try placing the Glide call inside onEvent callback

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

huangapple
  • 本文由 发表于 2020年6月5日 21:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/62216114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定