英文:
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("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);
}
}
My xml file
<?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>
My firestore collection
答案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("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);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论