【问题标题】:How to save a captured photo from unity in Android Image Gallery?如何在 Android Image Gallery 中保存统一拍摄的照片?
【发布时间】:2023-03-14 09:24:01
【问题描述】:

我有一个有效的脚本,可以用 AR 相机拍照并将其保存在 Android 的数据文件中,如下所示:

    screenShotName = "Dorot" + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".png";
    File.WriteAllBytes(Application.persistentDataPath + "/" + screenShotName, screenshot.EncodeToPNG());
    GameObject.Find("MainCanvas").GetComponent<Canvas>().enabled = true;

如何将 persistentDataPath 更改为 Android Image Gallery 的正确路径。

谢谢。

【问题讨论】:

  • 您无法将图片保存到图库,因为图库应用程序是一个应用程序,没有存储空间。图库应用程序只显示您设备上的图像。此外,您没有提及您使用的完整路径现在。
  • 谢谢,我明白了,目前我手机保存图片的路径是:我的文件\内部存储\安卓\数据\com.CompanyName.AppName\文件。在 Windows 上,图像保存在 AppData 文件夹中。
  • My files\ Internal storage\ Android\ data\ com.CompanyName.AppName\ files. 抱歉,这是 Android 设备上不存在的路径。在您的设备上使用更好的文件浏览器应用程序来找出真正的路径。

标签: c# android unity3d augmented-reality vuforia


【解决方案1】:

我正在使用 Unity Native Gallery Plugin 做同样的事情。

这是我的代码

public class ScreenshotTaker : MonoBehaviour
{
    public bool takingScreenshot = false;

    public void CaptureScreenshot()
    {
        StartCoroutine(TakeScreenshotAndSave());
    }

    private IEnumerator TakeScreenshotAndSave()
    {
        takingScreenshot = true;
        yield return new WaitForEndOfFrame();

        Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        ss.Apply();

        // Save the screenshot to Gallery/Photos
        string name = string.Format("{0}_Capture{1}_{2}.png", Application.productName, "{0}", System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
        Debug.Log("Permission result: " + NativeGallery.SaveImageToGallery(ss, Application.productName + " Captures", name));
        takingScreenshot = false;
    }
}

【讨论】:

    【解决方案2】:

    您可以按照 JackMini36 在他的回答中所说的使用插件来做到这一点。或者你可以写一段代码来做到这一点。

    但具有挑战性的部分将是让它在 Android 设备上跨平台和权限问题。这是一篇很好的描述文章,您可以在其中学习在统一应用程序中将屏幕截图保存在图库中的不同方法。 http://unitydevelopers.blogspot.com/2018/09/save-screenshot-in-unity3d.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      相关资源
      最近更新 更多