title: Saving Screenshot to Android Gallary via AR Apps
body: <p>so i am working on an ar application for android and i want to make screenshots of my ar scene in runtime and save them in the gallery.
  i create my app in unity 2018.1.6f1 with newest vuforia and arcore plugins on a samsung s8.
  i am pretty new to ar development and i want to prototype with both plugins and test the features of both plugins in the long run, plus i have basically no clue of programming android native features.</p>
  
  <p>knowing my lack of skill i searched for a solution in other posts and i found this:</p>
  
  <p><a href="https://stackoverflow.com/questions/44756917/saving-screenshot-to-android-gallary-via-game">Saving Screenshot to android gallary via game</a></p>
  
  <p>the solution in the answer works fine with vuforia but not at all with ar core. at this point i dont know how to make it run in ar core and i need help.</p>
  
  <p>i forgot to mention that it seems that the arcore application is capturing the screen but not saving the image in gallery or any other location i could find.</p>
  
  <p>i hope someone can hint me in the right direction or maybe fix the script.
  ty and sry for bad english :)</p>
  
  <pre><code>using System.Collections;
  using UnityEngine;
  
  namespace ARCoreDemo
  {
  public class InAppScreenShotToGallery : MonoBehaviour
  {
  
      protected const string _mediaStoreImageMedia = "android.provider.MediaStore$Images$Media";
      protected static AndroidJavaObject _activity;
  
  
      public void MakeScreenShot ()
      {
          StartCoroutine (CaptureScreenShot (Screen.width, Screen.height));
      }
  
  
      private IEnumerator CaptureScreenShot (int width, int height)
      {
          yield return new WaitForEndOfFrame ();
          Texture2D texture = new Texture2D (width, height);
          texture.ReadPixels (new Rect (0, 0, width, height), 0, 0);
          texture.Apply ();
  
          yield return texture;
          string path = SaveImageToGallery (texture, "Name", "Description");
          Debug.Log("Picture has been saved at;\n" + path);
  
      }
  
  
      protected static string SaveImageToGallery (Texture2D texture, string title, string description)
      {
          using (AndroidJavaClass mediaClass = new AndroidJavaClass(_mediaStoreImageMedia))
          {
              using (AndroidJavaObject contentResolver = Activity.Call&lt;AndroidJavaObject&gt;("getContentResolver"))
              {
                  AndroidJavaObject image = Texture2DToAndroidBitmap (texture);
                  return mediaClass.CallStatic&lt;string&gt;("insertImage", contentResolver, image, title, description);
              }
          }
      }
  
  
      protected static AndroidJavaObject Texture2DToAndroidBitmap(Texture2D texture)
      {
          byte[] encodedTexture = texture.EncodeToPNG();
          using (AndroidJavaClass bitmapFactory = new AndroidJavaClass("android.graphics.BitmapFactory"))
          {
              return bitmapFactory.CallStatic&lt;AndroidJavaObject&gt;("decodeByteArray", encodedTexture, 0, encodedTexture.Length);
          }
      }
  
  
      protected static AndroidJavaObject Activity
      {
          get
          {
              if (_activity == null)
              {
                  AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                  _activity = unityPlayer.GetStatic&lt;AndroidJavaObject&gt;("currentActivity");
              }
              return _activity;
          }
      }
  }
  </code></pre>
  
  <p>}</p>