0

this is my code so far.

 public void getimage() {
    FirebaseStorage storagi = FirebaseStorage.getInstance();
    StorageReference imagerefi = storagi.getReference();
        userimage = (ImageView) findViewById(R.id.imageViewuser);
        StorageReference pathReference = imagerefi.child("images/"+somename);

        imagerefi.child("images/"+somename).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                // Got the download URL for 'users/me/profile.png'
                uri2 = uri.toString();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle any errors
            }
        });



Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);


    }

I have tried without the success listener, and many other things. When i simply copy-paste the url from the firebase console, i get the image working perfectly. but the uri2 doesnt work as intended.

I basically want to get the file that is named after a firebase user uid and inside a folder called images inside the root directory of firebase storage, and display it on an imageview.

Thanks a lot for any help! I have been trying for weeks to solve this issue.

//some of the code has been recently modified from a stactoverflow post //therefore does nothing in this current configuration, i am aware of that

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141

2 Answers2

0

I wish somename is a valid name. try this

just move the line of set the image in imageview inside onSuccess to make sure the uri2 is the url from the firebaseStorage

public void getimage() {
    FirebaseStorage storagi = FirebaseStorage.getInstance();
    StorageReference imagerefi = storagi.getReference();
        userimage = (ImageView) findViewById(R.id.imageViewuser);
        StorageReference pathReference = imagerefi.child("images/"+somename);

        imagerefi.child("images/"+somename).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                // Got the download URL for 'users/me/profile.png'
                uri2 = uri.toString();
           Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
 }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle any errors

                Log.e("ERRORLOADING",exception.getMessage());
            }
        });





    }
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
0

To solve this, just move the following line of code:

Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);

Inside the callback right after the following line of code:

uri2 = uri.toString();

Remember, onSuccess() method has an asynchronous behaviour and if you try to use the value of the uri2 variable outside this method, it will always be null because by the time you are trying to access it, its value is not available yet. If you want to use it outside that method, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193