Android Firebase uploading images wrong URL (PROBLEM: X-Goog-Upload-Comment header is missing)

半腔热情 提交于 2020-03-03 07:28:09

问题


I'm trying to upload and download images from Firabase Database which has an URL link to Firebase Storage. The problem is that the strange URL is being saved to database (see the link at the bottom). What should I do to obtain a normal URL that I will be able to use do downloand the image into my Android app? Thank you in advance!

Here I post some code I use:

Upload to Firebase DataBase and Storage:

mStorageRef = FirebaseStorage.getInstance().getReference();
mDataBaseRef = FirebaseDatabase.getInstance().getReference();

if (mImageUri != null)
{
    final StorageReference fileReference = mStorageRef.child(nameimage + "." + getFileExtension(mImageUri));

    fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            Toast.makeText(AddAdvertisement.this, "Upload successful!", Toast.LENGTH_LONG).show();

            Upload upload = new Upload(et_localization, taskSnapshot.getUploadSessionUri().toString());
            String uploadId = mDataBaseRef.push().getKey();
            mDataBaseRef.child(uploadId).setValue(upload);


        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(AddAdvertisement.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

And download from Firebase:

databaseReference = FirebaseDatabase.getInstance().getReference();

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
        {
            Upload upload = postSnapshot.getValue(Upload.class);
            mUploads.add(upload);
        }

        mAdapter = new AdverisementAdapter(getContext(),mUploads);
        mrecyclerView.setAdapter(mAdapter);
    }

and Picasso to retreive image:

@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {

    Upload uploadCurrent = mUploads.get(i);

    imageViewHolder.textViewName.setText(uploadCurrent.getName());

    Picasso.get().load(uploadCurrent.getUrl()).into(imageViewHolder.imageView);
}

Picasso work fine, beacuse except form an image I also get from Firebase string with the name, which is downloaded appropriately. So, the problem I think it's just with this wrong url:

https://firebasestorage.googleapis.com/v0/b/my_name/o?name=image.jpg&uploadType=resumable&upload_id=AEnB2UrrOhqOVqTHuRRVRmlkf4Gh6y5xd_w5IvRok1SNVOMNnz34dqWFJ5_lPD0DNJr05mrHrT8g97sy0d4BZAdiB6v7skkLSQ&upload_protocol=resumable

When I try to entered this link, I receive this kind of error:

Invalid request. X-Goog-Upload-Command header is missing.


回答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(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

        Toast.makeText(AddAdvertisement.this, "Upload successful!", Toast.LENGTH_LONG).show();

        fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String url = uri.toString();
                Upload upload = new Upload(et_localization, url);
                String uploadId = 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).




回答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.



来源:https://stackoverflow.com/questions/54218220/android-firebase-uploading-images-wrong-url-problem-x-goog-upload-comment-head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!