Android Firebase上传图像的URL错误(问题:缺少X

编程入门 行业动态 更新时间:2024-10-17 02:55:37
本文介绍了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字符串中获得了名称正确下载的图像.所以,我认为问题出在这个错误的网址上:

firebasestorage.googleapis/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:

firebasestorage.googleapis/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

本文发布于:2023-11-14 19:59:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   错误   上传   Android   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!