android - Android(Unity + Firebase)上的选择图像和上传问题
问题描述
我的项目涉及使用 Unity + Firebase 并导出到 Android。我正在使用插件“Unimgpicker”从设备中选择图像,然后我想将其上传到 Firebase。在 Unity 中一切正常。选择图像适用于 android,但图像未上传。
我尝试将 image.texture -> Texture2D textureToUpload = imageToUpload.texture 作为 Texture2D 上传,在 Unity 中也可以正常工作。我更改了代码以涉及文件的路径,但它没有帮助
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Kakera;
using UnityEngine.UI;
using System.Threading;
using System.Threading.Tasks;
using Firebase;
using Firebase.Storage;
using Firebase.Database;
public class ImagePicker : MonoBehaviour
{
[SerializeField]
private Unimgpicker imagePick;
[SerializeField]
RawImage image;
[SerializeField]
RawImage imageToUpload;
Texture2D imageToUploadTexture;
[SerializeField]
Button uploadButton;
[SerializeField]
Text errorText;
void Awake()
{
// Unimgpicker returns the image file path.
imagePick.Completed += (string path) =>
{
StartCoroutine(LoadImage(path, image));
StartCoroutine(UploadAndroid(path));
};
}
public void OnPressShowPicker()
{
// With v1.1 or greater, you can set the maximum size of the image
// to save the memory usage.
imagePick.Show("Select Image", "unimgpicker", 1024);
}
private IEnumerator LoadImage(string path, RawImage output)
{
var url = "file://" + path;
var www = new WWW(url);
yield return www;
var texture = www.texture;
if (texture == null)
{
Debug.LogError("Failed to load texture url:" + url);
}
output.texture = texture;
}
public IEnumerator UploadAndroid(string path){
Debug.Log("Started");
if(path != string.Empty){
var url = "file://" + path;
var www = new WWW(url);
yield return www;
var texture = www.texture;
Texture2D imageToUpload = texture as Texture2D;
var bytesToSend = imageToUpload.EncodeToPNG();
Firebase.Storage.FirebaseStorage storage = Firebase.Storage.FirebaseStorage.DefaultInstance;
Firebase.Storage.StorageReference storage_ref = storage.GetReferenceFromUrl("gs://detectivepigv1.appspot.com");
Firebase.Storage.StorageReference images_ref = storage_ref.Child("Images/Test6.png");
images_ref.PutBytesAsync(bytesToSend).ContinueWith((Task<StorageMetadata> task) =>{
if(task.IsFaulted || task.IsCanceled){
Debug.Log(task.Exception.ToString());
errorText.text = task.Exception.ToString();
}
else{
Firebase.Storage.StorageMetadata metadata = task.Result;
string downloadUrl = images_ref.GetDownloadUrlAsync().ToString();
Debug.Log("Download at: " + downloadUrl);
}
});
}
else
{
Debug.Log("Choose Photo first");
}
}
}
我没有收到任何错误消息。将非常感谢任何建议。
解决方案
我无法对您的帖子添加任何评论,这就是我建议您回答的原因。首先,在您的第一个回调函数“LoadImage(path, image)”中检查存储路径是否正确,并且图像正在加载到输出纹理上。如果路径正确,那么我认为在代码中附加带有文件的路径存在一些问题
var url = "file://" + path 记录 url 并检查它。
其次,如果您有纹理格式的图像,那么您不需要从存储中获取文件(繁重的过程),您可以使用纹理的字节转换上传图像,下面是代码:
Firebase.Storage.FirebaseStorage storage = Firebase.Storage.FirebaseStorage.DefaultInstance;
byte[] bytes = imageToUpload.EncodeToJPG(); // your texture image
Firebase.Storage.StorageReference path_reference =
storage.GetReference("_img.jpg");
path_reference.PutBytesAsync(bytes)
.ContinueWith ((task) => {
if (task.IsFaulted || task.IsCanceled) {
Debug.Log(task.Exception.ToString());
// Uh-oh, an error occurred!
}
else
{
// Metadata contains file metadata such as size, content-type, and download URL.
path_reference .GetDownloadUrlAsync ().ContinueWith ((task1) =>
{
if (task1.IsFaulted || task1.IsCanceled) {
Debug.Log(task1.Exception.ToString());
Debug.Log("Oops!!!\nError in uploading image");
// Uh-oh, an error occurred!
}
else
{
var uri = task1.Result.OriginalString;
print("uri: "+uri);
}
});
}
});
希望这能解决您的问题。祝你好运!
推荐阅读
- java - 在 xtext 环境中将 JUnit4 升级到 JUnit5
- javascript - UnhandledPromiseRejectionWarning:错误:ENOTEMPTY:目录不为空
- uwp - 关于堆栈面板和树视图的 UWP 查询
- javascript - 如何使用 java 脚本、j 查询或 Angular 将文本从英语音译为阿拉伯语?
- mysql - 使用插入忽略查询时如何获取忽略的行
- amazon-web-services - 使用 AWS API Gateway 上的资源策略允许 IP 地址的特定 API 路径
- mysql - MySQL 日期字符串到新列中的日期格式
- twitter-bootstrap - 使用 Jinja 模板时,引导模式无法正确显示
- node.js - Electron - 构建应用程序时找不到模块“应用程序”
- javascript - 烧瓶 json 响应在 ajax 成功处理程序中失败