Android Firebase Uploading Images Wrong Url (problem: X-goog-upload-comment Header Is Missing)
Solution 1:
You're writing this value to the database:
taskSnapshot.getUploadSessionUri().toString()
This is the URI of the upload session, which you can use to resume an upload in case it gets aborted.
Since you want to store the download URL, this call is pretty useless for your cause. Instead you should call getDownloadUrl()
to (asynchronously) get the download URL for the newly uploaded file:
fileReference.putFile(mImageUri).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {
@OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(AddAdvertisement.this, "Upload successful!", Toast.LENGTH_LONG).show();
fileReference.getDownloadUrl().addOnSuccessListener(newOnSuccessListener<Uri>() {
@OverridepublicvoidonSuccess(Uri uri) {
Stringurl= uri.toString();
Uploadupload=newUpload(et_localization, url);
StringuploadId= mDataBaseRef.push().getKey();
mDataBaseRef.child(uploadId).setValue(upload);
}
});
}
})...
Note that this is quite well described in the Firebase documentation on getting a download URL after uploading a file, which event includes a sample of accomplishing the same by using continueWithTask
instead of nesting callbacks (which I did above).
Solution 2:
According to the official documentation regarding UploadTask.TaskSnapshot's getUploadSessionUri() method:
Returns the session Uri, valid for approximately one week, which can be used to resume an upload later by passing this value into
putFile(Uri, StorageMetadata, Uri)
.
I'm afraid that this is not the Uri
that you are looking for. To get the correct uri, please see my answer from this post.
Solution 3:
fileReference.putFile(mImageUri).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {
@OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(AddAdvertisement.this, "Upload successful!", Toast.LENGTH_LONG).show();
fileReference.getDownloadUrl().addOnSuccessListener(newOnSuccessListener<Uri>() {
@OverridepublicvoidonSuccess(Uri uri) {
// just do your task //like hashmaps to put in
}
});
}
})
Solution 4:
That did not worked for me (using Expo), but using XMLHttpRequest did. I've found this solution here: https://github.com/expo/expo/issues/2402#issuecomment-443726662
My code is below, in case it can help.
_uploadImageAsync = async (uri) => {
try {
const blob = awaitnewPromise((resolve, reject) => {
const xhr = newXMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
console.log(e);
reject(newTypeError('Network request failed'));
};
xhr.responseType = 'blob';
xhr.open('GET', uri, true);
xhr.send(null);
});
const ref = firebase
.storage()
.ref()
.child('images/usersPicture/test');
const snapshot = await ref.put(blob);
blob.close();
returnawait snapshot.ref.getDownloadURL();
} catch(error) {
console.log(error)
}
}
Post a Comment for "Android Firebase Uploading Images Wrong Url (problem: X-goog-upload-comment Header Is Missing)"