其实我正在尝试截取并将其保存到Android图库中,我正在使用一个开源库“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");
但是我从不保存屏幕截图,当我看到控制台时,我得到了2个重要的日志
1.我的调试日志“SecondLast”在控制台上打印但不是最后一个“Done screenshot”
2.有一个异常打印“UnauthorizedAccessException:访问路径”/storage/emulated/0/DCIM/GalleryTest/My_img_1.png“被拒绝。”
重点: - 我已经在统一播放器设置中将写入权限设置为外部(SDCard)。 (实际上我尝试使用“内部”和“外部”两种设置)
答案 0 :(得分:0)
我查看了他们的回购,使用您提供的源代码,似乎您正在尝试截取屏幕截图并保存,无论您是否拥有权限。这可能是您的问题:
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");