【发布时间】:2019-07-29 12:18:50
【问题描述】:
我的项目涉及使用 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");
}
}
}
我没有收到任何错误消息。将非常感谢任何建议。
【问题讨论】: