我已经设置了firebase项目,我可以成功地在两个用户之间交换聊天消息,但是我无法显示用户的 Profiles 图像 . 当我在web浏览器中打开这个url时会显示图像,但是如果我在android手机中使用这个url,则图像不会显示 .
我从其他活动上传图片,然后尝试从其他活动上传图片网址获取图片 .
下面是我用来从Firebase数据库中检索图像和其他数据的代码 . 我成功获得了上传图像的URL并将其传递到适配器中,我尝试使用Picasso或Glide通过使用此URL显示该图像 .
通过该URL图像在Web浏览器中成功显示,但无法在android中的imageview中显示此图像 .
DatabaseReference mFirebaseDatabaseReference=FirebaseDatabase.getInstance().getReference();
mFirebaseDatabaseReference.child(USERPROFILE_CHILD).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(FirebaseAuth.getInstance().getCurrentUser().getEmail().equalsIgnoreCase(dataSnapshot.child("email").getValue().toString())) {
} else {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("email", dataSnapshot.child("email").getValue().toString());
try {
// Got uploaded image url by using dataSnapshot.child("photourl").getValue() and put it in hashMap
hashMap.put("photourl",dataSnapshot.child("photourl").getValue().toString());
} catch (Exception e) {
e.printStackTrace();
}
hashMap.put("uid", dataSnapshot.child("uid").getValue().toString());
hashMap.put("firebaseToken",dataSnapshot.child("firebaseToken").getValue().toString());
testData.add(hashMap);
adapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我在imageview中加载图片的自定义适配器代码:---
final ImageView profileimage = (ImageView) v.findViewById(R.id.profileImageView);
try {
String imageUrl = data.get(position).get("photourl");
if (imageUrl.startsWith("gs://")) {
final StorageReference storageReference = FirebaseStorage.getInstance()
.getReferenceFromUrl(imageUrl);
storageReference.getDownloadUrl().addOnCompleteListener(
new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
String downloadUrl = task.getResult().toString();
System.out.println("====download url===="+downloadUrl);
/*Glide.with(context)
.load(downloadUrl)
.into(profileimage);*/ Picasso.with(context).load(downloadUrl).into(profileimage);
} else {
Log.w("thank you", "Getting download url was not successful.",
task.getException());
}
}
});
} else {
Picasso.with(context)
.load(imageUrl)
.into(profileimage);
}
} catch (Exception e) {
e.printStackTrace();
}
2 回答
干得好 . !
首先转到Firebase规则
然后添加Glide或picasso库 . !
尝试使用Picasso或Glide它会加载你的形象 .
This will help you use to picasso library