2

I can't retrieve my images in Firebase Storage, when I try to access the Url of the image, shows this message: "the X-Goog-Upload-Command header, which is not a valid X-Goog file". I can not access the files stored in Storage through the download URL. I ask for help, and the code bellow is how I'm uploading my files to the Storage and Database!

    if (pmcUri != null){

        // show the progress Dialog
        pmcProgress = new ProgressDialog(this);
        pmcProgress.setTitle("Creating your Feed");
        pmcProgress.setMessage("...");
        pmcProgress.show();

        final StorageReference mChildStorage = pmcStorage.child("feedPhotos")
                .child(pmcUri.getLastPathSegment());
        mChildStorage.putFile(pmcUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                final Uri downloadUri = taskSnapshot.getUploadSessionUri();

                FirebaseDatabase database = FirebaseDatabase.getInstance();


                mChildStorage.getDownloadUrl();
                // save infos in Database with user ID as a Reference
                final DatabaseReference myRef = database.getReference("feeds").child(uid.getUid());
                myRef.child("feed").setValue(setName);
                assert downloadUri != null;
                myRef.child("feedimg").setValue(downloadUri.toString());
                myRef.child("feedkey").setValue(uid.getUid());


                pmcProgress.dismiss();

                Intent intent = new Intent(create_feed.this, glime.class);
                startActivity(intent);
            }
        });

    }
KENdi
  • 7,576
  • 2
  • 16
  • 31
Miguel Nuno
  • 59
  • 10

2 Answers2

2

The way you're reading your downloadUri variable is with:

final Uri downloadUri = taskSnapshot.getUploadSessionUri();

Calling getUploadSessionUri() gives you a URI for th upload session, not the download URL for the file once it's finished uploading. The download URL is no longer available from the task snapshot, but instead requires an additional asynchronous call to the server.

When I have problems, I tend to fall back to the Firebase documentation, which has this handy example of getting the download URL after an upload has completed:

final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
        } else {
            // Handle failures
            // ...
        }
    }
});

So to get the download URL after the upload has completed, you start a new task that returns ref.getDownloadUrl().

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • It's working, thanks..I love you man, I'm learning code to improve my life and I was depressed that I couldn'tfix this problem, you save my year. THANK YOU SO MUCH. I wish you all the best and a Merry Christmas. – Miguel Nuno Dec 06 '18 at 16:55
1

Check firebase rules you can change like this to test

service firebase.storage {  match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write: if true;    }  }}
Tanveer Ahmed
  • 426
  • 1
  • 4
  • 15