首页 > 解决方案 > 将屏幕截图从持久数据路径移动到图库 - 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);
        }
 }
}

为什么这似乎适用于某些三星设备而不适用于其他设备?

标签: androidunity3dandroid-manifestandroid-permissionspersistent-storage

解决方案


推荐阅读