theBrainyGeek
theBrainyGeek

Reputation: 584

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.

Upvotes: 2

Views: 3923

Answers (2)

Ros&#225;rio P. Fernandes
Ros&#225;rio P. Fernandes

Reputation: 11326

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

Upvotes: 8

elbert rivas
elbert rivas

Reputation: 1464

You can also use Picasso library. Add this to your gradle

compile 'com.squareup.picasso:picasso:2.5.2'

Then to load the image,

Picasso.with(YourActivity.this).load(uri).into(image2);

Upvotes: 2

Related Questions