【问题标题】:Picking image and uploading problem on Android (Unity + Firebase)Android上的选择图像和上传问题(Unity + Firebase)
【发布时间】: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");
        }

    }
}

我没有收到任何错误消息。将非常感谢任何建议。

【问题讨论】:

    标签: android firebase unity3d


    【解决方案1】:

    我无法对您的帖子添加任何评论,这就是我建议您回答的原因。 首先,在您的第一个回调函数“LoadImage(path, image)”中检查存储路径是否正确,并且图像正在加载到输出纹理上。如果路径是正确的,那么我认为在您的代码中附加带有文件的路径存在一些问题

    var url = "file://" + 路径 记录 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);
    
    
    
                                }
                    });
    
            }
    });
    

    希望这能解决您的问题。 祝你好运!

    【讨论】:

    • 谢谢!来自路径的图像被加载到纹理中并在 Android 和 Unity 中显示没有问题这就是为什么我很困惑哈哈:)
    • 然后你可以使用图片字节转换来上传图片。我上面发布的代码对我来说很好用:) 你可以使用它
    • 不幸的是,就像以前一样 - 在统一中一切正常,上传在 Android 中不起作用。我开始认为我可能对 SDK 有问题。我安装了分析、存储和数据库,在 Firebase 控制台中添加了应用程序并将 json 文件放入项目中。我觉得我错过了什么。无论如何,再次感谢您的帮助! :)
    • 现在一切正常。我都可以选择图像并上传它。我没有改变任何东西。
    • 对你有好处:)
    猜你喜欢
    • 2019-12-20
    • 2017-09-18
    • 2017-11-21
    • 2021-05-12
    • 1970-01-01
    • 2014-08-30
    • 2018-07-04
    • 2014-10-13
    • 2013-10-12
    相关资源
    最近更新 更多