How to save a captured photo from unity in Android Image Gallery?
Asked Answered
K

2

5

I have a valid script that takes a photo with the AR camera and saves it in data files of Android, like so :

    screenShotName = "Dorot" + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".png";
    File.WriteAllBytes(Application.persistentDataPath + "/" + screenShotName, screenshot.EncodeToPNG());
    GameObject.Find("MainCanvas").GetComponent<Canvas>().enabled = true;

How can I change the persistentDataPath with the right path of Android Image Gallery.

Thanks.

Kenwood answered 11/5, 2018 at 8:37 Comment(3)
You cannot save pictures to the Gallery as the Gallery app is an app and no storage place.The Gallery app just shows images which are on your device.. Further you did not menion the full path you use now.Cajole
Thanks, I understand, The current path of saving images in my phone is: My files\ Internal storage\ Android\ data\ com.CompanyName.AppName\ files. On Windows, the Images are saved in the AppData folder.Kenwood
My files\ Internal storage\ Android\ data\ com.CompanyName.AppName\ files. Sorry but that is a non existing path on an Android device. Use a better file explorer app on your device to find out the real path.Cajole
J
6

I am using Unity Native Gallery Plugin to do the same exact thing.

This is my code

public class ScreenshotTaker : MonoBehaviour
{
    public bool takingScreenshot = false;

    public void CaptureScreenshot()
    {
        StartCoroutine(TakeScreenshotAndSave());
    }

    private IEnumerator TakeScreenshotAndSave()
    {
        takingScreenshot = true;
        yield return new WaitForEndOfFrame();

        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();

        // Save the screenshot to Gallery/Photos
        string name = string.Format("{0}_Capture{1}_{2}.png", Application.productName, "{0}", System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
        Debug.Log("Permission result: " + NativeGallery.SaveImageToGallery(ss, Application.productName + " Captures", name));
        takingScreenshot = false;
    }
}
Jada answered 11/5, 2018 at 10:0 Comment(0)
S
0

You can do it as JackMini36 said in his answer by using the plugin. Or you can write you piece of code do that.

But the challenging part will be to make it cross platform and permission issues on Android devices. Here is nicely describe article where you can learn different way to save screenshot in gallery in unity applications. http://unitydevelopers.blogspot.com/2018/09/save-screenshot-in-unity3d.html

Sawyers answered 21/4, 2021 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.