android - 将屏幕截图从持久数据路径移动到图库 - Android
问题描述
我一直在使用下面的脚本截屏,将其保存到持久数据路径,然后将其移动到图库并刷新图库。
在 Samsung Gallery S8 和 Samsung Tab S5e 上进行测试时,效果很好;我截取屏幕截图,然后我可以在设备库中查看它。
但是,我现在已经在三星 Galaxy A50 上对其进行了测试,但它不起作用(我也收到了其他有同样问题的人的报告)。它保存到 Persistent Data Path 但无法将其移动到 DCIM/Camera 文件夹,出现错误:
2020-12-04 10:51:56.345 6166-6251/? E/Unity: UnauthorizedAccessException: Access to the path is denied.
at System.IO.File.Move (System.String sourceFileName, System.String destFileName) [0x00000] in <00000000000000000000000000000000>:0
at SaveGallery+<CRSaveScreenshot>d__2.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0
(Filename: currently not available on il2cpp Line: -1)
myDefaultLocation 返回以下路径:
/storage/emulated/0/Android/data/BUNDLEIDENTIFIER/files/Screenshot104958.jpg
myScreenshotLocation 返回以下路径:
/storage/emulated/0/DCIM/Camera/Screenshot104958.jpg
这是我正在使用的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
public class SaveGallery : MonoBehaviour
{
void Start()
{
if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
// The user authorized use of the External Storage.
}
else
{
// We do not have permission to use the External Storage.
// Ask for permission or proceed without the functionality enabled.
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
}
public void TakePhoto(){
StartCoroutine(CRSaveScreenshot());
}
IEnumerator CRSaveScreenshot()
{
yield return new WaitForEndOfFrame();
string myFileName = "Screenshot" + System.DateTime.Now.Hour + System.DateTime.Now.Minute + System.DateTime.Now.Second + ".jpg";
string myDefaultLocation = Application.persistentDataPath + "/" + myFileName;
string myFolderLocation = "/storage/emulated/0/DCIM/Camera/"; //EXAMPLE OF DIRECTLY ACCESSING A CUSTOM FOLDER OF THE GALLERY
string myScreenshotLocation = myFolderLocation + myFileName;
Debug.Log("I WANT TO SAVE TO " + myScreenshotLocation);
//ENSURE THAT FOLDER LOCATION EXISTS
if (!System.IO.Directory.Exists(myFolderLocation))
{
System.IO.Directory.CreateDirectory(myFolderLocation);
}
ScreenCapture.CaptureScreenshot(myFileName);
//MOVE THE SCREENSHOT WHERE WE WANT IT TO BE STORED
yield return new WaitForSeconds(3);
System.IO.File.Move(myDefaultLocation, myScreenshotLocation);
using (AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject joActivity = jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (AndroidJavaObject joContext = joActivity.Call<AndroidJavaObject>("getApplicationContext"))
using (AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))
using (AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"))
using (AndroidJavaObject joExDir = jcEnvironment.CallStatic<AndroidJavaObject>("getExternalStorageDirectory"))
{
jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { myScreenshotLocation }, null, null);
}
}
}
为什么这似乎适用于某些三星设备而不适用于其他设备?
解决方案
推荐阅读
- react-admin - 将 ReferenceArrayInput 与非平面数组一起使用
- python - 导入 Boost Python 模块时出错 (function_impl_base9max_arityEv)
- c++ - 使用 occi 库将 c++ 程序与 Oracle 连接
- android - 布局问题并帮助我解决在所有设备中扩展的问题
- sql-server - 在 ubuntu 上配置 MSSQL Server - 无法打开或读取持久注册表:\SystemRoot\security.hiv
- rrdtool - 如何创建具有特定时间的rrd文件?
- javascript - 无法创建 HTML
- authentication - 如何实现跨域单点登录
- java - 更新 Intellij 2019.1 后未指定模块
- ios - 通过 Facebook 登录不适用于我的 iOS 应用