Android Firebase上传图像的URL错误(问题:缺少X-Goog-Upload-Comment标头) [英] Android Firebase uploading images wrong URL (PROBLEM: X-Goog-Upload-Comment header is missing)

查看:140
本文介绍了Android Firebase上传图像的URL错误(问题:缺少X-Goog-Upload-Comment标头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firabase数据库上载和下载图像,该数据库具有指向Firebase Storage的URL链接.问题是奇怪的URL被保存到数据库中(请参阅底部的链接).我应该怎么做才能获得一个可以使用的普通URL,将图片下放到我的Android应用程序中?预先谢谢你!

在这里发布一些我使用的代码:

上传到Firebase数据库和存储:

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();
        }
    });

并从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);
    }

和毕加索一起获得图像:

@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可以正常工作,因为除形成图像外,我还从Firebase字符串中获得了名称正确下载的图像.所以,我认为问题出在这个错误的网址上:

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

当我尝试输入此链接时,会出现这种错误:

无效的请求.缺少X-Goog-Upload-Command标头.

解决方案

您正在将此值写入数据库:

taskSnapshot.getUploadSessionUri().toString()

这是上传会话的URI,您可以使用它来恢复上传,以防其中止.

由于您要存储下载URL,因此对于您的原因,此调用几乎没有用.相反,您应该调用getDownloadUrl()来(异步)获取新上载文件的下载URL:

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);
            }
        });

    }
})...

请注意,这在Firebase 有关获取 .上传文件后下载URL.,该事件包括通过使用continueWithTask而不是嵌套回调(如上所述)来实现相同目的的示例.

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.

解决方案

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).

这篇关于Android Firebase上传图像的URL错误(问题:缺少X-Goog-Upload-Comment标头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆