如何从Firebase存储下载图像? [英] How to download image from Firebase storage?

查看:210
本文介绍了如何从Firebase存储下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用以下示例从Firebase存储中下载和使用图像:


I'm trying to download and use image from my Firebase storage using this example:

如何从firebase数据库下载图像,然后将其统一加载到图像中

跟随调试查找问题:

UnityEngine.UnityException:SupportsTextureFormatNative只能从主线程调用.

UnityEngine.UnityException: SupportsTextureFormatNative can only be called from the main thread.

请提供,如何解决?或者,也许您可​​以提供其他方式来下载和使用图像.
谢谢!

Can you offer please, how to solve it? Or maybe you can offer different way to download and use image.
Thanks!

我的代码:

  var firebase = FirebaseStorage.DefaultInstance;
    var storageRef = firebase.GetReferenceFromUrl(_urlPics);

    storageRef.Child(_resourceName).GetBytesAsync(1024*1024).ContinueWith(task =>
    {
        if (task.IsCompleted)
        {
            var texture = new Texture2D(2, 2);
            byte[] fileContent = task.Result;

            texture.LoadImage(fileContent);
            var newRect = new Rect(0.0f, 0.0f, texture.width, texture.height);
            var sprite = Sprite.Create(texture, newRect, Vector2.zero);
            _image.sprite = sprite;
            Debug.Log("FINISH DOWNLOAD");
        }
        else
        {
            print("DOWNLOAD WRONG");
        }
    });

从以下位置抛出错误:

var texture = new Texture2D(2, 2);

推荐答案

对此我很快得到了很好的解释(感谢Pux0r3):

I shortly got a nice explenation on this (thanks goes out to Pux0r3):

这是次要更新,但将ContinueWith替换为

This is a minor update, but replacing ContinueWith with ContinueWithOnMainThread will improve this slightly.

在.NET 4.x运行时中,延续可能经常在后台执行,而此扩展方法会将其推送到主线程.

With the .NET 4.x runtime, continuations may often execute in the background, whilst this extension method pushes them onto the main thread.

.NET 3.x运行时不存在此问题,因为连续性包含在Parse库中,而隐式做到这一点.

The .NET 3.x runtime didn't have this issue as continuations were contained within the Parse library, which did this implicitly.

另请参阅 Firebase和Tasks,如何在Unity中处理异步逻辑,以获取有关此方法和替代实现方式的更多信息.

Also see the Firebase and Tasks, how to deal with asynchronous logic in Unity for more information on this and alternative implementation ways.

因此,以前的ContinueWith工作正常".为了确保并且不必使用例如来实现自定义工作器 ConcurrentQueue<Action> (一种常用的方法通过将ContinueWith替换为

So while former ContinueWith "just worked". In order to be sure and without having to implement a custom worker using e.g. a ConcurrentQueue<Action> (an often used approach for dispatching callbacks to the main thread in Unity) you should be fine by simply replacing ContinueWith with ContinueWithOnMainThread from the Firebase.Extensions.TaskExtension.

这篇关于如何从Firebase存储下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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