You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hololens应用拍照保存至图片库失败及报错求助

Fixing HoloLens Photo Save to Pictures Library & "Windows Does Not Exist" Error

Hey there! Let's get your photos saving to the HoloLens Pictures Library properly, and fix that annoying namespace error along the way. Here's a step-by-step breakdown:

1. Why You're Seeing the "Windows Does Not Exist" Error

This usually pops up for one of two reasons:

  • Your project isn't configured for the Universal Windows Platform (UWP) (HoloLens runs UWP apps exclusively).
  • You're missing the required namespace reference, or Unity's scripting environment isn't set up to recognize UWP-specific APIs.

2. First, Configure Your Unity Project Correctly

Before tweaking code, make sure your project is set up for HoloLens deployment:

  • Go to File > Build Settings, select Universal Windows Platform, and click Switch Platform.
  • In Player Settings > Other Settings:
    • Set Scripting Runtime Version to .NET 4.x Equivalent (this is required for async/await and UWP API compatibility).
  • In Player Settings > Publishing Settings > Capabilities:
    • Check the Pictures Library box—this grants your app permission to access the device's photo library.

3. Replace Your Path-Based Code with UWP's Storage API

You can't directly use KnownFolders.PicturesLibrary.Path because of UWP's sandbox security model. Instead, use the asynchronous StorageFolder and StorageFile APIs to save your photo safely.

Here's a working code example:

using UnityEngine;
using System.Threading.Tasks;
// Only include UWP namespaces when building for HoloLens, not in the editor
#if UNITY_WSA && !UNITY_EDITOR
using Windows.Storage;
#endif

public class PhotoSaver : MonoBehaviour
{
    // Call this method with your captured photo bytes and desired filename
    public async void SavePhotoToLibrary(byte[] photoBytes, string filename)
    {
#if UNITY_WSA && !UNITY_EDITOR
        try
        {
            // Get access to the HoloLens Pictures Library
            StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
            
            // Create or replace the file (use ReplaceExisting to overwrite duplicates)
            StorageFile photoFile = await picturesFolder.CreateFileAsync(
                filename, 
                CreationCollisionOption.ReplaceExisting
            );
            
            // Write the photo data to the file
            await FileIO.WriteBytesAsync(photoFile, photoBytes);
            
            Debug.Log($"Successfully saved photo to: {photoFile.Name}");
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Failed to save photo: {ex.Message}");
        }
#else
        Debug.LogWarning("Photo saving only works on HoloLens/UWP builds, not in the Unity Editor.");
#endif
    }
}

4. How to Use This Code

When you capture your photo (using HoloLens's camera APIs), pass the raw byte array and a filename (with a valid extension like .jpg) to the SavePhotoToLibrary method. For example:

// Example: After capturing a photo into a byte array
byte[] capturedPhoto = ...; // Your photo data here
GetComponent<PhotoSaver>().SavePhotoToLibrary(capturedPhoto, $"HoloCapture_{System.DateTime.Now:yyyyMMddHHmmss}.jpg");

Key Notes

  • Always use async/await for UWP file operations—they're asynchronous by design, and blocking calls can cause performance issues or crashes.
  • The #if UNITY_WSA && !UNITY_EDITOR preprocessor directives prevent Unity's editor from throwing errors about missing Windows APIs (since the editor runs on Windows/macOS, not HoloLens).
  • Make sure your filename includes a valid image extension (.jpg, .png) so the HoloLens Photos app recognizes and displays it.

Once you build and deploy this to your HoloLens, your photos should show up in the device's Photos app right away!

内容的提问来源于stack exchange,提问作者Bhavani Gudimetla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:08:11