ScreenCapture.CaptureScreenshot doesn't work

Hi, I am trying to take a screen shot within magic leap. By debugging, I am sure that I have already reach the line of ScreenCapture.CaptureScreenshot(filePath), and the folder was created. However, after execution of the app, there was no picture saved in the folder. Here is the code:

        public void CaptureAndSaveScreenshot() {
            DebugText.text = "Start Coroutine";
            StartCoroutine(CaptureScreenshotCoroutine());
        }

        private IEnumerator CaptureScreenshotCoroutine() {
            yield return new WaitForEndOfFrame();
            string filePath = GetScreenshotPathWithTimestamp();
            string directory = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(directory)) {
                Directory.CreateDirectory(directory);
            }
            DebugText.text = "before screenshot";
            ScreenCapture.CaptureScreenshot(filePath);
            DebugText.text = "finish";
        }

        private string GetScreenshotPathWithTimestamp() {
            string dateTimeFormat = System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
            string filename = dateTimeFormat + ".png";

            #if UNITY_EDITOR
            string folderPath = Path.Combine(Application.dataPath, "Screenshots");
            #elif UNITY_ANDROID || UNITY_IOS
            string folderPath = Path.Combine(Application.persistentDataPath, "Screenshots");
            #else
            string folderPath = Path.Combine(Application.dataPath, "Screenshots");
            #endif

            return Path.Combine(folderPath, filename);
        }

The CaptureAndSaveScreenshot() is called in update when I triggered the controller. Any hints/idea about why it doesn't is highly appreciate.

I recommend adding additional debug statements, then checking the logs to see if the script completes successfully by reading the logs. You should also check if you are running into any errors.

DebugText.text = "before screenshot";
DebugText.text = "finish";

These lines in my code are for debug printing. They both printed out.
Is that sufficient to prove the ScreenCapture.CaptureScreenshot(filePath); is already called successfully? Meanwhile, I also used debug print to check and show that the filePath is exist.

For the log, do you mean by the log in Device bridge?

Printing out the file location might help you locate where is it being saved on device. Additionally, you can capture the logs via ADB or device bridge through the Magic Leap Hub. You can also use the Unity ADB log cat package.

Hi, I tried but they are not work. The CaptureScreenshot didn't give any error output. Is that possible that some Magic Leap setting that disable saving images?

Magic Leap does not limit saving files outside of the standard constraints of Android. To further diagnose, do you mind conducting a file write test? This will pinpoint whether the issue is with the Unity screenshot functionality or broader file writing permissions on Magic Leap 2.

** Simple Text File Script:**

private void TestWriteFile() {
    string path = Path.Combine(GetScreenshotPathWithTimestamp(), "testFile.txt");
    string content = "Test file for write permissions.";
    string directory = Path.GetDirectoryName(path);
    if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
    try {
        File.WriteAllText(path, content);
        Debug.Log($"Test file written to: {path}");
    } catch (Exception ex) {
        Debug.LogError($"Test file write failed: {ex.Message}");
    }
}

Implement this method and call it under the same conditions as your screenshot capture. If the test file is successfully written but screenshots still fail, this indicates a potential issue with Unity's screenshot mechanism. If the test file also fails, this points to broader file access issues in your application.

Hi, I have codes like this in other scripts:

        string filePath = util.getPath(filename);
        Debug.Log(filePath);
        string directory = Path.GetDirectoryName(filePath);

        if (!Directory.Exists(directory)) {
            Directory.CreateDirectory(directory);
        }

        File.WriteAllText(filePath, sb.ToString());

This code write sb.ToString to a csv file and it works on my magic leap.

When Using the following script in the latest Unity magic Leap Examples resulted in a screenshot without any issues. Make sure you request storage permissions

using System.Collections;
using UnityEngine;

public class ScreenshotTest : MonoBehaviour
{
    // Call this method to take a screenshot and save it to local app storage
    public void Start()
    {
        StartCoroutine(CaptureScreenshotCoroutine());
    }

    private IEnumerator CaptureScreenshotCoroutine()
    {
        yield return new WaitForSeconds(3);
        // Define the filename and path for the screenshot
        string screenshotFilename = "screenshot.png";
        string screenshotPath = Application.persistentDataPath + "/" + screenshotFilename;

        // Capture the screenshot
        ScreenCapture.CaptureScreenshot(screenshotFilename);

        // Wait for the end of the frame to ensure the screenshot has been captured
        yield return new WaitForEndOfFrame();

        // Optionally, output the screenshot path to the console
        Debug.Log("Screenshot saved to: " + screenshotPath);
    }
}

Hi, in the case of storage permission, do you mean by this setting in unity? I didn't found other permission settings.

Since you mentioned being able to write a csv file to the application data path, you most likely would not need to add any additional permissions.

For reference, magic leap 2 uses the standard Android Storage permission and workflow.

Hi, I have tested your code on both my project and the magic leap example 1.20 (my project is build under similar environment of magic leap example1.20) And although the image was saved successfully, it only has a small pixel. (like below graph)
screenshot1

However, this code works perfectly with magic leap example 2.0. (like below graph)

What's different in the projects lead to this different performance? Is that because the different unity version used by these projects? (1.20 use unity 2022.2.0.f1 and 2.0 using 20223.11.f1)

Hi, after I force switch the unity version to 3.11f1, the screen shot works but my webview rendering page no longer works. The whole WebViewScreen just disappear and even without the white panel at first. But when I compile the whole project again, it works perfectly. Thank you!

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.