Home>

In-app images on Firebase Storage using Cloud Storage for Unity
I want to send it.

The following is complete.
・ Google-services file for Android is already set in Assets
-SDK (Unity Package) for Cloud Storage has also been imported
・ Firebase console application registration and Cloud Storage have already been created.
・ Images folder has been created in Cloud Storage

When I test it with the Unity simulator, the images are in the images folder without any problem.
It will be stored.

But in the Android device test I installed the app on Android
Even if I test it like a simulator, it is not stored for some reason.

By the way, Firebase Auth authentication and storage in RealTimeDatabase are possible.
Why doesn't only Android device testing work?

Please press the one you understand.

environment)
PC: Windows 10
Unity 2019.4.0f1
Language: C #

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using Firebase.Storage;
using System;
public class SendDisplay: MonoBehaviour
{
    [SerializeField] Image sendImage = null;
    [SerializeField] Text nickNameText = null;
    [SerializeField] InputField inputComment = null;// usrCommentMessage
    [SerializeField] Dropdown DdLevel = null;// Level
    [SerializeField] GameObject DialogNoBox = null;
    [SerializeField] GameObject DialogSended = null;
    List<PhotoType>photoList;
    string path;
    public int indexNo;
    DatabaseReference _FirebaseDB;// Hold the object with the reference to the DB.
    FirebaseStorage _Storage;
    StorageReference storage_ref;
    StorageReference thumbNailFile_ref;
    Firebase.Auth.FirebaseUser _FirebaseUser;// Hold the credentials when connecting to Firebase.
    Firebase.Auth.FirebaseAuth auth;
    string userId;
    string nickName;
    string commentString;
    void Start ()
    {auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        indexNo = PlayerPrefs.GetInt ("indexno", 0);
        nickName = PlayerPrefs.GetString ("nickname", "");
        nickNameText.text = nickName;
        photoList = new List<PhotoType>();
        photoList = PlayerPrefsUtility.LoadList<PhotoType>("photolist");
        path = Application.persistentDataPath + "/" + photoList [indexNo] .FileName + ".png";
        sendImage.sprite = LoadScript (path);
        AuthLogin ();// Firebase Auth authentication
        // Set an instance of Firebase Database
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl ("https://hogehoge.firebaseio.com/");
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
        // Set an instance of Firebase Storage
        _Storage = FirebaseStorage.DefaultInstance;
        storage_ref = _Storage.GetReferenceFromUrl ("gs: //hogehoge.appspot.com");
    }
    // Image display
    Sprite LoadScript (string path)
    {
        if (string.IsNullOrEmpty (path)) return null;
        if (System.IO.File.Exists (path))
        {
            byte [] bytes = System.IO.File.ReadAllBytes (path);
            Texture2D texture2D = new Texture2D (1, 1);
            texture2D.LoadImage (bytes);
            Sprite sprite = Sprite.Create (
                      texture2D,
                      new Rect (0,0, texture2D.width, texture2D.height),
                      new Vector2 (0.5f, 0.5f));
            return sprite;
        }
        return null;
    }
    public void OnClickStorageTest ()
    {
        // Specify the thumbnail image save destination
        thumbNailFile_ref = storage_ref.Child ("images /" + _FirebaseUser.UserId + photoList [indexNo] .FileName + ".png");
        // Specify the thumbnail image save source and send
        thumbNailFile_ref.PutFileAsync (path) .ContinueWith ((System.Threading.Tasks.Task<StorageMetadata>task) =>{
                if (task.IsFaulted || task.IsCanceled) {
                    // error handling
                } else {
                    StorageMetadata metadata = task.Result;
                    // success processing
                }
        });
    }
    public void OnClickSend ()
    {inputComment = inputComment.GetComponent<InputField>();
        commentString = inputComment.text;
        if (commentString == "")
        {
            DialogNoBox.SetActive (true);
        } else {
            _FirebaseDB = FirebaseDatabase.DefaultInstance.GetReference ("Lists");
            Dictionary<string, object>itemMap = new Dictionary<string, object>();
            itemMap.Add ("Day", GetDateTime ());
            itemMap.Add ("FileName", _FirebaseUser.UserId + indexNo.ToString ("D3"));
            itemMap.Add ("Level", DdLevel.value);
            itemMap.Add ("UsrComment", commentString);
            itemMap.Add ("Sender", nickName);
            itemMap.Add ("Flag", 1);
            Dictionary<string, object>map = new Dictionary<string, object>();
            map.Add (_FirebaseUser.UserId + indexNo.ToString ("D3"), itemMap);
            _FirebaseDB.UpdateChildrenAsync (map);
            DialogSended.SetActive (true);
        }
    }
    void AuthLogin ()
    {
        auth.SignInAnonymouslyAsync (). ContinueWith (task =>{
            if (task.IsCanceled) {
                Debug.LogError ("SignInAnonymouslyAsync was canceled.");
                return;
            }
            if (task.IsFaulted) {
                Debug.LogError ("SignInAnonymouslyAsync encountered an error:" + task.Exception);
                return;
            }
            _FirebaseUser = task.Result;
            Debug.LogFormat ("User signed in succesfully: {0} ({1})",
            _FirebaseUser.DisplayName, _FirebaseUser.UserId);
            userId = _FirebaseUser.UserId;
            Debug.Log ("authid:" + userId);
            // PlayerPrefs.SetString ("authid", userId);
            });
            PlayerPrefs.SetString ("authid", "0000");
            PlayerPrefs.Save ();
    }
    int GetDateTime ()
    {
        DateTime now = DateTime.Now;
        return now.Year * 10000 + now.Month * 100 + now.Day;
    }
}