Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set Firebase Image Uri in Image View

I am retrieving an image from my firebase database and setting it in an Image View. I am using the following code.

mStorageRef.child("Book_Photos/"+firstBook.bid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Toast.makeText(getApplicationContext(), "GET IMAGE SUCCESSFUL",Toast.LENGTH_LONG).show();
                if(uri==null){
                    Toast.makeText(getApplicationContext(), "URI IS NULL",Toast.LENGTH_LONG).show();
                }
                try {
                    ImageView image2;
                    image2=(ImageView)findViewById(R.id.imageView);
                    image2.setImageURI(null);
                    image2.setImageURI(uri);

                }
                catch (Exception e){

                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), "GET IMAGE FAILED",Toast.LENGTH_LONG).show();
                // Handle any errors
            }
        });

The image I am retrieving is not being set. The "GET IMAGE SUCCESSFUL" toast works. The "URI IS NULL" toast does not work. The command image2.setImageURI(null) works.

Just image2.setImageURI(uri) is not working.

like image 803
theBrainyGeek Avatar asked Jan 25 '26 16:01

theBrainyGeek


1 Answers

You can't load images from the internet directly to an ImageView. But you can use a library like Glide to do it. To add Glide to your app, you need to add this line to your dependecies:

implementation 'com.github.bumptech.glide:glide:4.8.0'

And to load your image:

Glide
    .with(getContext())
    .load(uri) // the uri you got from Firebase
    .centerCrop()
    .into(image2); //Your imageView variable
like image 67
Rosário Pereira Fernandes Avatar answered Jan 27 '26 06:01

Rosário Pereira Fernandes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!