Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#unity脚本,用于在android gallery中使用库UnityNativeGallery拍摄并保存屏幕截图_C#_Android_Unity3d - Fatal编程技术网

C#unity脚本,用于在android gallery中使用库UnityNativeGallery拍摄并保存屏幕截图

C#unity脚本,用于在android gallery中使用库UnityNativeGallery拍摄并保存屏幕截图,c#,android,unity3d,C#,Android,Unity3d,实际上,我正在尝试截图并将其保存到Android gallery中,为此我使用了一个开源库“Unity Native gallery”,我的代码如下: NativeGallery.Permission permission = NativeGallery.CheckPermission(); if (permission == NativeGallery.Permission.Granted) { Debug.Log("May proceed"); } else { Deb

实际上,我正在尝试截图并将其保存到Android gallery中,为此我使用了一个开源库“Unity Native gallery”,我的代码如下:

NativeGallery.Permission permission = NativeGallery.CheckPermission();
if (permission == NativeGallery.Permission.Granted) {
     Debug.Log("May proceed");
 }
else {
     Debug.Log("Not allowed");
}
// Output ==> "May Proceed"

Debug.Log("Path is "+NativeGallery.GetSavePath("GalleryTest","My_img_{0}.png"));
//Output ==>  /storage/emulated/0/DCIM/GalleryTest/My_img_1.png

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();

Debug.Log("Secondlast");
permission = NativeGallery.SaveImageToGallery( ss, "GalleryTest", "My_img_{0}.png" ) ;

Debug.Log("Done screenshot");
但我的it从未保存屏幕截图,当我看到控制台时,我会得到2条重要日志

1.我的调试日志“SecondLast”打印在控制台上,但不是最后一个“Done screenshot”

2.打印的异常“UnauthorizedAccessException:访问路径”//storage/emulated/0/DCIM/GalleryTest/My_img_1.png”被拒绝


要点:-我已经在unity player设置中将写入权限设置为外部(SD卡)。(实际上,我尝试使用“内部”和“外部”两种设置)

我查看了他们的回购协议,使用您提供的源代码,似乎您正在尝试截图并保存它,无论您是否拥有权限。这可能是您的问题:

NativeGallery.Permission permission = NativeGallery.CheckPermission();
if (permission == NativeGallery.Permission.Granted) 
{
     Debug.Log("May proceed");
}
else 
{
     Debug.Log("Not allowed");
     // You do not break out of the function here so it will attempt to save anyways
}
您可能应该让它看起来像这样:

NativeGallery.Permission permission = NativeGallery.CheckPermission();
if (permission == NativeGallery.Permission.ShouldAsk) 
{
     permission = NativeGallery.RequestPermission();
     Debug.Log("Asking");
}
// If we weren't denied but told to ask, this will handle the case if the user denied it.
// otherwise if it was denied then we return and do not attempt to save the screenshot
if (permission == NativeGallery.Permission.Denied) 
{
     Debug.Log("Not allowed");
     return;
}
Debug.Log("Path is "+NativeGallery.GetSavePath("GalleryTest","My_img_{0}.png"));
//Output ==>  /storage/emulated/0/DCIM/GalleryTest/My_img_1.png

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();

Debug.Log("Secondlast");
permission = NativeGallery.SaveImageToGallery( ss, "GalleryTest", "My_img_{0}.png" ) ;

Debug.Log("Done screenshot");

我相信您需要在尝试使用这些权限之前请求这些权限。仅仅因为你在Unity Player设置中设置了它并不意味着玩游戏的人给了你权限。另外,您只告诉我们日志消息中的哪一条第一条消息在哪里?很抱歉错过了第一次输出,现在我更新问题。由于输出为“可能继续”。@eddge实际上是NativeGallery.SaveImageToGallery()方法在内部调用RequestPermission方法。但正如您所说,我们必须请求许可,但我从未看到任何对话请求gallery的许可。感谢brother的快速回复,实际上是NativeGallery.SaveImageToGallery()方法在内部调用此RequestPermission方法。