0

I'm trying to load an image from Firebase Storage, put it inside an ImageView and set it to the bottom navigation bar icon, here's my code:

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);'

but the setIcon method can only receive a drawable file, how do I solve this problem?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Yassine
  • 55
  • 5

1 Answers1

0

If you need to set the image that you from downloadUrl to the icon that exists in the BottomNavigation, then you can convert it like this:

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());
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I'm getting an error at `InputStream inputStream = (InputStream) url.getContent();` and the app crashes. – Yassine May 03 '22 at 12:18
  • What is the exact error that you get? What is the message you see in the logcat? Have you added the corresponding imports? – Alex Mamo May 03 '22 at 12:38
  • Have you solved the issue? – Alex Mamo May 04 '22 at 05:49
  • Yes I added the corresponding imports here's the error that I'm getting: https://imgur.com/a/poHhbmc – Yassine May 04 '22 at 08:14
  • Have you tried [this](https://stackoverflow.com/a/66300622/5246885) out? Give it a try and tell me if it works. – Alex Mamo May 04 '22 at 08:25
  • Yes I did try moving my code to a thread before, still got the same error – Yassine May 04 '22 at 08:42
  • Without seeing what you have already tried, I cannot be much of a help. So what exactly have you done? – Alex Mamo May 04 '22 at 08:47
  • I tried the answers provided here: https://stackoverflow.com/questions/58285700/bottomnavigationview-set-custom-icon-downloaded-from-some-url But it still doesn't work, the shape of the icon changes corresponding to the picture but no image is loaded inside it. – Yassine May 04 '22 at 08:52
  • I'm sorry, "doesn't work" doesn't provide enough information so I can help. In general, other questions (issues) that derive from the initial question should be considered new questions and should be added separately here on StackOverflow separately. So please post a new question, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo May 04 '22 at 08:57
  • I posted another question explaining what I tried to do in more details, however I don't think this is a problem of firebase storage anymore. https://stackoverflow.com/questions/72110631/loading-an-image-inside-a-bottom-navigation-bar-item-icon-using-an-url – Yassine May 04 '22 at 09:25
  • I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo May 04 '22 at 09:28
  • 1
    Can I help you with other information regarding the initial issue? – Alex Mamo May 04 '22 at 10:09
  • I got a solution in the new post that I added, thank you for sticking with me much appreciated! – Yassine May 04 '22 at 10:13