Java 如何使用firestore中的图像url从firebase存储获取图像
我正试图在glide的帮助下从firebase存储中获取我的图像。我还从firebase中获取用户名,并显示出来。当我在glide中直接使用url时,图像不会显示出来。但我不能这样做,因为我首先要将url分配给字符串,然后在glide中使用字符串。但是当我这样做时,没有图像显示出来起来 我的java代码Java 如何使用firestore中的图像url从firebase存储获取图像,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我正试图在glide的帮助下从firebase存储中获取我的图像。我还从firebase中获取用户名,并显示出来。当我在glide中直接使用url时,图像不会显示出来。但我不能这样做,因为我首先要将url分配给字符串,然后在glide中使用字符串。但是当我这样做时,没有图像显示出来起来 我的java代码 public class profile extends AppCompatActivity { CircleImageView circleImageView; TextVi
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);
}
}
公共类配置文件扩展了AppCompatActivity{
CircleImageView CircleImageView;
文本视图全名;
弗斯;
FirebaseFirestore商店;
字符串用户标识;
底部导航视图底部导航视图;
字符串uri;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
fAuth=FirebaseAuth.getInstance();
fStore=FirebaseFirestore.getInstance();
circleImageView=findViewById(R.id.profilepicture2);
fullname=findviewbyd(R.id.nameinprofile);
userId=fAuth.getCurrentUser().getUid();
bottomNavigationView=findViewById(R.id.bottom_navigation2);
bottomNavigationView.SetSelectedItem(R.id.profilelogo);
bottomNavigationView.setOnNavigationItemSelectedListener(新的bottomNavigationView.OnNavigationItemSelectedListener(){
@凌驾
公共布尔值onNavigationItemSelected(@NonNull MenuItem item){
开关(item.getItemId()){
案例R.id.profilelogo:
返回true;
案例R.id.home:
startActivity(新意图(getApplicationContext(),homepage.class));
返回true;
案例R.id.addlogo:
startActivity(新意图(getApplicationContext(),search.class));
返回true;
}
返回false;
}
});
最终文档引用=fStore.collection(“Userdata”).document(userId);
documentReference.addSnapshotListener(这个,新的EventListener(){
@凌驾
public void OneEvent(@Nullable DocumentSnapshot DocumentSnapshot,@Nullable FirebaseFirestoreException e){
setText(documentSnapshot.getString(“全名”);
uri=documentSnapshot.getString(“imageUrl”);
}
});
使用(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);
}
});
final DocumentReference=fStore.collection(“Userdata”).document(userId);
documentReference.addSnapshotListener(这个,新的EventListener(){
@凌驾
public void OneEvent(@Nullable DocumentSnapshot DocumentSnapshot,@Nullable FirebaseFirestoreException e){
setText(documentSnapshot.getString(“全名”);
uri=documentSnapshot.getString(“imageUrl”);
Glide.with(YourActivity.this).load(uri).into(circleImageView);
}
});
谢谢你给我打电话。我试着照你说的做,但没有效果,图像视图是空白的。