C#unity脚本使用库UnityNativeGallery在android库中获取并保存屏幕截图

weixin_38050219 2019-09-12 12:29:57
实际上我正在尝试截取并将其保存到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"); 但是我从来没有保存屏幕截图,当我看到控制台时,我得到了两个重要的日志 1.我的调试日志“SecondLast”在控制台上打印但不是最后一个“Done screenshot” 2.有一个异常打印“UnauthorizedAccessException:访问路径”/storage/emulated/0/DCIM/GalleryTest/My_img_1.png“被拒绝.” 重要的一点: – 我已经在统一播放器设置中将写入权限设置为外部(SDCard). (实际上我尝试使用“内部”和“外部”两种设置)
...全文
493 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38069603 2019-09-12
  • 打赏
  • 举报
回复
我看了他们的回购,你提供了源代码,看来你正试图截取并保存它,无论你是否拥有权限.这可能是您的问题: 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");

473

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧