Unity3D: Image is not saving in HDD

122

Application.CaptureScreenshot() has atleast 2 subtle use issues that come to mind:

  1. On iOS it won't accept a path, it will only write to the Documents folder. So your "SavedScreen.png" is going to be in:

    Application.persistentDataPath + "/screenshot.png";

  2. It is asynchronous, so the file will take a few frames (if not a second) to write to the file system. And it doesn't tell you when it is finished.

I prefer the full control of ReadPixels, but if you want to use CaptureScreenshot, you should wait atleast 2 seconds before you access the file:

void CaptureScreenshot()
{
  StartCoroutine(CaptureScreenshotCo());
}
IEnumerator CaptureScreenshotCo()
{
  string file = "screenshot.png";
  string path = string.Format("{0}/{1}", Application.persistentDataPath, file);
  // capture screenshot async 
  Application.CaptureScreenshot(file);
  // wait for file to be saved
  yield return new WaitForSeconds(2f);
  // now do something with file...
}
Share:
122

Related videos on Youtube

Author by

Sharath Kumar

Updated on November 27, 2022

Comments

  • Sharath Kumar about 1 month

    I am working on an mobile app using camera. My issue is the image snapped is not saving in the app root folder. Here is the snippet of code am using to capture the screen.

    Application.CaptureScreenshot("SavedScreen.png");
    

    But this doesn't saving anything in the internal storage.Can any one help me out. Cheers.

    • Chris McFarland
      Chris McFarland about 8 years
    • Sharath Kumar about 8 years
      I have tried all that. But my snap is not saving. Do should I grant any permissions for that. @Chris
    • peterept
      peterept about 8 years
      Which platform are you on?