问题
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);
}
});
}
回答1:
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:
- How to use getdownloadurl in recent versions?, which shows the simplest possible snippet to get a download URL, and itself links to even more questions about this.
- Firebase Storage getDownloadUrl() method can't be resolved, which is the error you probably got leading you to use
getUploadSessionUri()
. - getDownloadURL isn't inputting the link i need
回答2:
Check firebase rules you can change like this to test
service firebase.storage { match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true; } }}
来源:https://stackoverflow.com/questions/53652406/url-generated-by-firebase-storage-does-not-allow-access-to-the-file