Can't save screenshot into persistentDataPath
Asked Answered
P

2

1

It works great in the Editor, but on Android device I got an error that this file can not be written.

AndroidPlayer([email protected]:34999) Failed to store screen shot (/storage/emulated/0/Android/data/com.DefaultCompany.Test3D/files/storage/emulated/0/Android/data/com.DefaultCompany.Test3D/files/Screenshots/LastPhoto.png)

What’s wrong ?

I use this code:

public static string MakeScreenShot()
{
 
string directoryPath =Path.Combine(Application.persistentDataPath ,"Screenshots");
if(!Directory.Exists(directoryPath))
{    
    //if it doesn't exist, create it
    Directory.CreateDirectory(directoryPath); 
}
 
string fPath = Path.Combine(directoryPath,"LastPhoto.png");
ScreenCapture.CaptureScreenshot(fPath);
Debug.Log("Screenshot created " + fPath);
return fPath;
}
Pricefixing answered 22/6, 2024 at 14:11 Comment(2)

I can write the file with File command - same path, no problems var tex = new Texture2D(100, 100, TextureFormat.RGB24, false); var bytes = tex.EncodeToPNG(); File.WriteAllBytes(Path.Combine(directoryPath,"LastPhoto1.png"), bytes); But ScreenCapture.CaptureScreenshot doesn't work. What's up ?

Pricefixing

Did you find something about this? I'm having the same problem with UWP.

Dooryard
A
0

You can use this and try

string fileName = dateTime.ToFileTime() + ".png";

#if UNITY_EDITOR
ScreenCapture.CaptureScreenshot( Application.persistentDataPath + "/" + fileName);
#else
ScreenCapture.CaptureScreenshot(fileName);
#endif
Airwoman answered 22/6, 2024 at 14:9 Comment(0)
L
0

By using this answer and your code:

using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class ScreenShot : MonoBehaviour
{
    [SerializeField]
    Canvas canvas;
    public void OnClickScreenCaptureButton()
    {
        StartCoroutine(CaptureScreen());
    }

    public IEnumerator CaptureScreen()
    {
        // Wait till the last possible moment before screen rendering to hide the UI
        yield return null;
        canvas.enabled = false;

        // Wait for screen rendering to complete
        yield return new WaitForEndOfFrame();

        string fileName = GetAndroidExternalStoragePath() + "/screenshot_" + DateTime.Now.Ticks + ".png";
        Debug.Log("[Ario] "+ fileName);
        // Take screenshot
        var tex = ScreenCapture.CaptureScreenshotAsTexture();
        var bytes = tex.EncodeToPNG(); 
        File.WriteAllBytes(fileName, bytes);
       // ScreenCapture.CaptureScreenshot(fileName);

        // Show UI after we're done
        canvas.enabled = true;
    }
    private string GetAndroidExternalStoragePath()
    {
        if (Application.platform != RuntimePlatform.Android)
            return Application.persistentDataPath;

        var jc = new AndroidJavaClass("android.os.Environment");
        var path = jc.CallStatic<AndroidJavaObject>("getExternalStoragePublicDirectory",
            jc.GetStatic<string>("DIRECTORY_DCIM"))
            .Call<string>("getAbsolutePath");
        return path;
    }
}
Lyman answered 22/6, 2024 at 14:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.