如何将图像视图设置为图标?



我正在尝试从Firebase Storage加载图像,将其放入ImageView并将其设置为底部导航栏图标,这是我的代码:

DocumentReference df = fstore.collection("Users").document(user.getUid());
df.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful())
{
DocumentSnapshot doc = task.getResult();
if (doc.exists())
{
if (doc.get("profilePictureUrl")!= null) { //set profile pic into image view
String downloadUrl = doc.get("profilePictureUrl").toString();
Glide.with(BottomNavigationActivity.this).load(downloadUrl).into(profileImg);
}
}
}
}
});
//set icon only accepts a drawable file
bottomNav.getMenu().getItem(4).setIcon(profileImg);'

但是setIcon方法只能接收一个可绘制的文件,我如何解决这个问题?

如果你需要将downloadUrl的图像设置为底部导航栏中存在的图标,那么你可以像这样转换它:

if (doc.get("profilePictureUrl")!= null) { //set profile pic into image view
String downloadUrl = doc.get("profilePictureUrl").toString();
Glide.with(BottomNavigationActivity.this)
.load(downloadUrl)
.into(profileImg);
try {
URL url = new URL(downloadUrl);
InputStream inputStream = (InputStream) url.getContent();
Drawable icon = Drawable.createFromStream(inputStream, "src name");
bottomNav.getMenu().getItem(4).setIcon(icon);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}

相关内容

  • 没有找到相关文章

最新更新