0
\$\begingroup\$

so I have a method for taking a screenshot, it works just fine on PC, but I've tried everything and cannot get it to work on Android. I've tried creating custom folders, saving to persistentDataPath, saving to dataPath, and none of it works.

I had a way of testing it which said the name of the screenshot, logically it should keep the name as screenshot1 if it fails to take the screenshot, which it does on mobile, whereas the one on desktop increments properly.

Here's the method I most recently attempted...

    public void captureScreenshot(string name)
    {
        if (!countDown)
        {
            for (int i = 1; i < 500; i++)
            {

                if (!File.Exists(Application.dataPath + name + i + ".png"))
                {
                    if (!Directory.Exists(Application.dataPath + "/appScreenshots"))
                    {
                        Directory.CreateDirectory(Application.dataPath + "/appScreenshots");
                    }
                    ScreenCapture.CaptureScreenshot(Application.dataPath + name + i + ".png");
                    break;
                }
            }
        }
    }

    public void captureScreenshot(string name)
    {
        if (!countDown)
        {
            for (int i = 1; i < 500; i++)
            {

                if (!File.Exists(Application.dataPath + name + i + ".png"))
                {
                    if (!Directory.Exists(Application.dataPath + "/appScreenshots"))
                    {
                        Directory.CreateDirectory(Application.dataPath + "/appScreenshots");
                    }
                    ScreenCapture.CaptureScreenshot(Application.dataPath + name + i + ".png");
                    break;
                }
            }
        }
    }

So, it works just fine on PC, just not on mobile... Do I need to get special permissions or something?

Thanks

\$\endgroup\$
6
  • \$\begingroup\$ The documentation for ScreenCapture.CaptureScreenshot mentions that on mobile the filename parameter is appended to Application.persistentDataPath. Have you tried with just the filename? \$\endgroup\$
    – user35344
    May 4, 2021 at 10:25
  • \$\begingroup\$ I just tried that now, but it's still not saving properly. Again I added a line to print of the screenshot name, it increments on Pc but not on mobile. \$\endgroup\$
    – Makineko
    May 4, 2021 at 10:43
  • \$\begingroup\$ Application.dataPath is not a writeable location on mobile. Did you mean to use persistentDataPath? It also looks to me like you might be missing a slash between your path and your file name, and you're not using the appScreenshots folder you're creating. Is the directory created as expected when using persistentDataPath? \$\endgroup\$
    – DMGregory
    May 4, 2021 at 11:35
  • \$\begingroup\$ Yeah, I've tried both. And I noticed the slash, there's actually a slash in the "name" string. \$\endgroup\$
    – Makineko
    May 4, 2021 at 16:30
  • \$\begingroup\$ So just to clarify, just having ScreenCapture.CaptureScreenshot("Test") somewhere does not produce any output for you on Android? \$\endgroup\$
    – user35344
    May 6, 2021 at 6:43

1 Answer 1

0
\$\begingroup\$

Well, I fixed it. I think the issue was that I had a / in the name, I tried test but nothing happened, so i tried just "Screenshot.png" and it worked, now it's taking screenshots. Here's the updated code,

        public void captureScreenshot(string name)
    {

            for (int i = 1; i < 500; i++)
            {

                if (!File.Exists(Application.persistentDataPath + "/Screenshot.png" + (i + 1)))
                {
                    ScreenCapture.CaptureScreenshot("Screenshot" + (i + 1)+".png");

                    text.GetComponent<TextMeshProUGUI>().text = name + i + ".png";
                    break;
                }
            
             }
    }
\$\endgroup\$
3
  • \$\begingroup\$ You probably want the (i + 1) before the .png - maybe like $"Screenshot{i + 1}.png" instead. \$\endgroup\$
    – DMGregory
    May 7, 2021 at 13:57
  • \$\begingroup\$ Thank you, I should've posted after I fixed that haha. \$\endgroup\$
    – Makineko
    May 10, 2021 at 10:44
  • \$\begingroup\$ It's still not too late to fix it now. Also, don't forget to click the checkmark icon to mark this answer as "Accepted" if it worked for you. \$\endgroup\$
    – DMGregory
    Oct 4, 2021 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .