Reputation: 3
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 doesn't 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 I'm 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
instead of this, which does work when i put get manually from the firebase storage on the firebase console
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.
Upvotes: 0
Views: 125
Reputation: 138999
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);
}
});
}
});
Upvotes: 1