Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android Studio Firebase Storage getDownloadUrl() doesnt have token

I can successfully upload an image to firebase storage, but when i try to retrieve the image using storage reference and getDownloadUrl() i get only the images url that doesnt include the token, without the token i cannot display the image, i tried googling how to properly retrieve link from firebase storage and it seems i did it correctly (for the record im trying to use that url to save it in as a string in a firestore field that contains a string that is the link)

example:
im getting this:

https://firebasestorage.googleapis.com/XX/X/XXXXXXXXXXXXXX/X/XXXXXXXXXXX

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

instead of this, which does work when i put get manually from the firebase storage on the firebase console

https://firebasestorage.googleapis.com/XX/X/XXXXXXXXXXXXXX/X/XXXXXXXXXXX?alt=media&token=XXXXXXXXXXXXXXXXXXXXXXXXX

this is how i get the user to pick an image

    private void openFileChooser()
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

        activityResultContracts.launch(intent);
    }

then here
this is how i get the image uri


    ActivityResultLauncher<Intent> activityResultContracts = registerForActivityResult(new  ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if(result.getResultCode() == Activity.RESULT_OK)
            {
                mImageUri = result.getData().getData();
                uploadFile();
            }
        }
    });

and finally here
this is the upload file function

    private void uploadFile()
    {
        if(mImageUri != null)
        {
            StorageReference fileReference = storageRef.child("star_images/" + System.currentTimeMillis() + "." + getFileExtension(mImageUri));
            fileReference.putFile(mImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.    TaskSnapshot> task) {
                    task.getResult().getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            db.document("stars/" + starId).update("imagePath", uri.toString());
                        }
                    });
                }
            });
        }
        else
        {
            Snackbar.make(findViewById(R.id.starProfileLayout),"No file selected.", Snackbar.LENGTH_SHORT).show();
        }
    }
}

(in my firestore database
the star has a field which is a string named imagePath

in my firebase storage
the images are inside the a folder called star_imageS)

i just cant understand whats wrong.

(edited: added blockquotes)

>Solution :

To get the downloadUrl, there is no need to use:

 task.getResult().getMetadata().getReference().getDownloadUrl().addOnSuccessListener(/* ... /*);

That’s because you’re only reading the reference of the image. task.getResult().getMetadata() returns an object of type StorageMetadata. Calling getReference() on such an object returns an object of type StorageReference which returns:

the associated StorageReference for which this metadata belongs to.

So it seems that you are running around in a circle since you get the reference to the image itself, not the downloadUrl which contains the token.

To solve this, you simply need to use the following lines of code:

fileReference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String downloadUrl = uri.toString();
                Log.d("TAG", downloadUrl);

                //Update the downloadUrl in Firestore.
                db.document("stars/" + starId).update("imagePath", downloadUrl);
            }
        });
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading