C# 如何在unity中的android上保存图像

C# 如何在unity中的android上保存图像,c#,android,image,unity3d,C#,Android,Image,Unity3d,我是Unity的新手,我正试图保存一张图片,以便在以后得到它。我尝试Application.captureshreenshot和使用Texture2D.ReadPixel的方法,我尝试保存在persistentDataPath(/data/user/0/my.package.name/files/)、/sdcard/Download/和/storage/emulated/0/Download中。没有办法奏效。在我项目的每个清单中,我都有写入外部存储权限。若我保存到持久数据,我并没有找到它,我只找

我是Unity的新手,我正试图保存一张图片,以便在以后得到它。我尝试Application.captureshreenshot和使用Texture2D.ReadPixel的方法,我尝试保存在persistentDataPath(/data/user/0/my.package.name/files/)、/sdcard/Download/和/storage/emulated/0/Download中。没有办法奏效。在我项目的每个清单中,我都有写入外部存储权限。若我保存到持久数据,我并没有找到它,我只找到UnityAds的缓存,若我保存到下载文件夹,我会被拒绝访问

有人能帮我吗

这是我的密码:

IEnumerator ScreenShot(){
         yield return new WaitForEndOfFrame ();
         Application.CaptureScreenshot ("ball.png");
         Application.CaptureScreenshot ("/sdcard/Download/ball.png");
         int width = Screen.width;
         int height = Screen.height;
         Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
         tex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
         tex.Apply ();
         byte[] bytes = tex.EncodeToJPG ();
         File.WriteAllBytes ("sdcard/Download/ball.png", bytes);
         File.WriteAllBytes (Application.persistentDataPath + "/ball.png", bytes);
 }
在检查文件夹是否存在之前:

if(!System.IO.Directory.Exists("/sdcard/Download/"))
{ 
    System.IO.Directory.CreateDirectory ("/sdcard/Download/"); 
}

if(!System.IO.Directory.Exists(Application.persistentDataPath))
{
    System.IO.Directory.CreateDirectory (Application.persistentDataPath);
}
我从以下内容开始:

StartCoroutine(ScreenShot());

我做错了什么?

以下是适合我的:

    string destination = DateTime.Now.ToString ("yyyy-MM-dd-HHmmss") + ".png";
    string fullDestination = Path.Combine (Application.persistentDataPath, destination);

    Application.CaptureScreenshot (destination);

    SharingWriter.WriteTextInstant ("Preparing...");

    FileInfo fileInfo = new FileInfo(fullDestination);
    while(fileInfo == null || fileInfo.Exists == false)
    {
        yield return null;
        fileInfo = new FileInfo(fullDestination);
    }

Application.captureshreenshot处理所有事情,无需担心读取像素和写入冗余文件。只是有一个循环,然后在协同程序中等待魔术发生!我用它来创建手机屏幕截图共享功能。

你确定它没有保存吗?你试过读取路径上的文件吗?是的,我转到路径/Android/data/mypackge/并且没有文件文件夹,只有unityads的缓存,下载文件夹中也没有。我认为用户无法读取路径,但如果我与路径共享此图像,我将返回黑屏,并且图像不存在。捕获截图保存在Application.persistentDataPath中。您可以使用它来检索文件名为的数据。好的,我尝试了,但在Android/data/mypackage/中手动找不到,这很正常吗?