locked
Error while loading image from Windows Phone gallery into my Unity application RRS feed

  • Question

  • Hi,

    I have developped a Windows Phone plugin for my Unity application which enable me to open the Windows Phone gallery in order to select an image and get its physic path.

    I've used this path to load the image in the application with a WWW object but I get this error

    "Error. Operation has failed with error 0x80070005: access is denied."

    The path is correct because I display it and I've ticked the ID_CAP_MEDIALIB_PHOTO in the WMAppManifest.xml (Capabilities tab).

    Here is my Unity C# test code to load the image with the path.

    IEnumerator loadImage()
    {
         this.guiText.text = filePath;
         WWW www = new WWW(filePath);
         yield return www;
     
         if(www.error == null)
         {
             GameObject texture = GameObject.Find("UNITY");
             if(texture != null)
             {
                 texture.guiTexture.texture = www.texture;
             }
         }
     }

    Any help would be appreciated ! Thanks in advance !
    Friday, April 17, 2015 1:08 PM

All replies

  • The error is correct. Your app doesn't have direct access to that path. It can directly load files only from its app data and install directories.

    To load from the libraries I it needs to use the StorageFile to get brokered access.

    The easiest way may be to copy from the library to local data then use WWW to load that. Otherwise you'll probably need to wrap the StorageFile can for use in unity. Unity has such a wrapper in the Unity Engine.WSA namespace that you can try

    Friday, April 17, 2015 1:25 PM
  • Thanks to have replied. What do I exactly have to copy, I didn't really understand your answer, can you add more details ? Thanks in advance !
    Friday, April 17, 2015 2:01 PM
  • Hi LittlePandaz,

    What Rob suggests is either you copy the image from the picture gallery into the app local folder and then load your freshly copied local image using WWW OR you can create a wrapper to the Windows Platform as Unity did with the WSA API.

    Here is an example on how to implement such a wrapper :

    using UnityEngine;
    using System.Collections;
    using System;
    
    #if NETFX_CORE
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Threading.Tasks;
    using Windows.Storage;
    #endif
    
    public class MyScript : MonoBehaviour {
    
        string PICTURE_LIB_FOLDER_NAME = "Camera Roll";
        string IMAGE_FILENAME = "Box3_Bump.png";
    
        byte[] myImageBuffer = null;
    
        void Start() 
        {
            LoadImageFromGallery(IMAGE_FILENAME);      
        }
    
    
    #if NETFX_CORE
    
        async void LoadImageFromGallery(string filename)
        {
            myImageBuffer = await loadImageFromCameraRollAsync(filename);
        }
    
        async Task<byte[]> loadImageFromCameraRollAsync(string filename)
        {
            byte[] bytes = null;
    
            // Get the Camera Roll subfolder
            StorageFolder cameraRollFolder = await KnownFolders.PicturesLibrary.GetFolderAsync(PICTURE_LIB_FOLDER_NAME);
    
            StorageFile file = await cameraRollFolder.GetFileAsync(filename);
    
            var buffer = await FileIO.ReadBufferAsync(file);
    
            bytes = buffer.ToArray();
    
            return bytes;
        }
    #else
        void LoadImageFromGallery(string filename)
        {
            // Dummy function for Unity Editor
        }
    #endif
    
        // Update is called once per frame
        void Update () {
    	
            if(myImageBuffer != null)
            {
                var tex = new Texture2D(2, 2);
    
                tex.LoadImage(myImageBuffer);
    
                gameObject.renderer.material.mainTexture = tex;
            }
        }
    }

    This exemple works fine with light image (less than 20KB). If you start applying a texture with an image coming from the phone camera (which can reach up to few MB), you should consider to resize it before in order to avoid performance and memory issue. Hope this help. 

    Thursday, May 7, 2015 2:47 PM