Saving Screenshot to android gallary via game

若如初见. 提交于 2019-12-24 11:51:35

问题


It takes the screenshot but doesn't show in gallery . the screenshot is saved to android/data/com.company.name/file.name but I want to save it directly to the gallery with filename screenshot

So far, here my code is:

public void Capture() 
{
    string filename = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
    Application.CaptureScreenshot(filename + ".jpg");
    Debug.Log("captured screenshot");
}

回答1:


Look for the answer provided here (the second one). It works perfectly.

Here's the final code:

protected const string MEDIA_STORE_IMAGE_MEDIA = "android.provider.MediaStore$Images$Media";
protected static AndroidJavaObject m_Activity;

protected static string SaveImageToGallery(Texture2D a_Texture, string a_Title, string a_Description)
{
    using (AndroidJavaClass mediaClass = new AndroidJavaClass(MEDIA_STORE_IMAGE_MEDIA))
    {
        using (AndroidJavaObject contentResolver = Activity.Call<AndroidJavaObject>("getContentResolver"))
        {
            AndroidJavaObject image = Texture2DToAndroidBitmap(a_Texture);
            return mediaClass.CallStatic<string>("insertImage", contentResolver, image, a_Title, a_Description);
        }
    }
}

protected static AndroidJavaObject Texture2DToAndroidBitmap(Texture2D a_Texture)
{
    byte[] encodedTexture = a_Texture.EncodeToPNG();
    using (AndroidJavaClass bitmapFactory = new AndroidJavaClass("android.graphics.BitmapFactory"))
    {
        return bitmapFactory.CallStatic<AndroidJavaObject>("decodeByteArray", encodedTexture, 0, encodedTexture.Length);
    }
}

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

And you simply call:

string path = SaveImageToGallery(picture, "Test Picture", "This is a description.");

EDIT: Since you seem really new to Unity I'd suggest learning it first. Anyway here's how you can call the code provided above:

public void CaptureScreenshot()
{
    StartCoroutine(CaptureScreenshotCoroutine(Screen.width, Screen.height));
}

private IEnumerator CaptureScreenshotCoroutine(int width, int height)
{
    yield return new WaitForEndOfFrame();
    Texture2D tex = new Texture2D(width, height);
    tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    tex.Apply();

    yield return tex;
    string path = SaveImageToGallery(tex, "Name", "Description");
    Debug.Log("Picture has been saved at:\n" + path);
}

Simply add those two methods to your code and call the CaptureScreenshot() either from another script, a Unity Button, or anything else...

Hope this helps,



来源:https://stackoverflow.com/questions/44756917/saving-screenshot-to-android-gallary-via-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!