Save image in VR Quest 2 - Screenshot cam

I need to take a picture from the game camera (screenshot) and save the image in VR.

Take a screenshot I got it in several ways.

The biggest problem is saving the image in VR.

A screenshot only appears when I run it in Unity game mode.

I did a test in Start() and passing a sprite just to try to save in VR based on this tutorial but without success:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;

public class ScreenAndroid : MonoBehaviour
{
    //public AndroidExtensions Screen1 = new AndroidExtensions();

    public Texture2D Imagem;
    public string title;
    public string description;



    private void Start()
    {
        AndroidExtensions.SaveImageToGallery(Imagem, title, description);

        var appName = Application.identifier;
        var fileName = $"{appName}-{DateTime.Now:yyMMdd-hhmmss}";



#if UNITY_ANDROID
        AndroidExtensions.SaveImageToGallery(Imagem, fileName, "Some description");
#endif
    }
}

8523995--1137077--upload_2022-10-19_0-39-43.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[SerializeField]
public class AndroidExtensions
{
    private static AndroidJavaObject _activity;

    private static AndroidJavaObject Activity
    {
        get
        {
            if (_activity != null) return _activity;
            var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            _activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            return _activity;
        }
    }

    private const string MediaStoreImagesMediaClass = "android.provider.MediaStore$Images$Media";

    public static string SaveImageToGallery(Texture2D texture2D, string title, string description)
    {
        using var mediaClass = new AndroidJavaClass(MediaStoreImagesMediaClass);
        using var cr = Activity.Call<AndroidJavaObject>("getContentResolver");
        var image = Texture2DToAndroidBitmap(texture2D);
        var imageUrl = mediaClass.CallStatic<string>("insertImage", cr, image, title, description);
       
        Debug.Log("imageUrl");
        return imageUrl;
    }

    private static AndroidJavaObject Texture2DToAndroidBitmap(Texture2D texture2D)
    {
        var encoded = texture2D.EncodeToPNG();
        using var bf = new AndroidJavaClass("android.graphics.BitmapFactory");
        return bf.CallStatic<AndroidJavaObject>("decodeByteArray", encoded, 0, encoded.Length);
    }
}

Android manifest:
8523995--1137080--upload_2022-10-19_0-40-30.png

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools">

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
    <application android:requestLegacyExternalStorage="true">
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:theme="@style/UnityThemeSelector">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>
    </application >
</manifest>

What isnt working?
The screenshot itself or the saving it locally?

If you want to save in a directory then maybe also enable external storage access in player settings?

Saving it locally wasn’t working

In the previous script I’m trying to pass a sprite to Quest 2 for testing.

I changed, I will test
8524889--1137251--upload_2022-10-19_8-47-49.png

I solved the doubts. I made a different code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PictureCam : MonoBehaviour
{
    public Camera eyedropperCamera;

    private Texture2D eyedropperTexture;
    private Rect eyeRect;
    private bool firstGetPixel = true;
    private RenderTexture eyedropperRenderTexture;

    public void Trigger1()
    {
        StartCoroutine(TriggerTimeX());
    }

    public IEnumerator TriggerTimeX()
    {
        yield return new WaitForSeconds(2);
        StartCoroutine(GetPixel(true));
        yield return new WaitForSeconds(1);
        StartCoroutine(GetPixel(true));
    }

    public IEnumerator GetPixel(bool saveImage)
    {
        yield return new WaitForEndOfFrame();

        if (firstGetPixel)
        {
            firstGetPixel = false;

            eyedropperTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
            eyeRect = new Rect(0, 0, Screen.width, Screen.height);

            eyedropperRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
            eyedropperRenderTexture.Create();

            eyedropperCamera.targetTexture = eyedropperRenderTexture;
            eyedropperCamera.enabled = false;
        }

        RenderTexture currentRT = RenderTexture.active;
        try
        {
            RenderTexture.active = eyedropperCamera.targetTexture;
            eyedropperCamera.backgroundColor = eyedropperCamera.backgroundColor;
            eyedropperCamera.transform.position = eyedropperCamera.transform.position;
            eyedropperCamera.transform.rotation = eyedropperCamera.transform.rotation;
            eyedropperCamera.Render();
            eyedropperTexture.ReadPixels(new Rect(0, 0, eyedropperCamera.targetTexture.width, eyedropperCamera.targetTexture.height), 0, 0);
        }
        finally
        {
            RenderTexture.active = currentRT;
        }

        string name = "Screenshot_EpicApp" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";

        NativeGallery.SaveImageToGallery(eyedropperTexture, "Myapp pictures", name);
    }

}

i got try your function

hi I put in your code its working in oculus quest2 but, if i take a screenshot app screen was lagged