4

This method is supposed to return the downloadUrl but keeps returning null and the weird part is, I can see the URL in the log

private String profileUrlPath(byte[] profile){

    mStorageReference.child("card_profile").child(userId+".jpg").putBytes(profile).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            profilepathref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    if(uri != null){
                        profileUrl = uri.toString();
                        Log.d(TAG, "profile image Url: " + profileUrl);
                    }


                }
            });

        }
    });

    return profileUrl;

}
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
mulira
  • 53
  • 5
  • 1
    Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Aug 24 '18 at 12:29

2 Answers2

1

You register a success handler on an asychronous task. There is no guarantee when the line profileUrl = uri.toString(); is executed.

There is a very high probability, that it is not before returning from profileUrlPath.

Furthermore, there might be concurrency issues.

jokster
  • 577
  • 5
  • 14
0

Do Like this

 private String profileUrlPath(byte[] profile){

        mStorageReference.child("card_profile").child(userId+".jpg").putBytes(profile).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                profilepathref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        if(uri != null){
                            profileUrl = uri.toString();
                            Log.d(TAG, "profile image Url: " + profileUrl);
                             return profileUrl;
                        }


                    }
                });

            }
        });

        return null;

    }
Learning Always
  • 1,563
  • 4
  • 29
  • 49