通过游戏将屏幕截图保存到android画廊 [英] Saving Screenshot to android gallary via game

查看:180
本文介绍了通过游戏将屏幕截图保存到android画廊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要截图,但不会显示在gallery中.屏幕截图已保存到android/data/com.company.name/file.name,但我想使用文件名屏幕截图直接将其保存到图库中

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");
}

推荐答案

寻找提供的答案

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

这是最终代码:

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;
    }
}

您只需致电:

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

因为您似乎对Unity真的很陌生,所以建议您先学习它.无论如何,您可以通过以下方式调用上面提供的代码:

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

只需将这两种方法添加到您的代码中,然后从另一个脚本,Unity Button或其他任何内容中调用CaptureScreenshot() ...

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

希望这会有所帮助,

这篇关于通过游戏将屏幕截图保存到android画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆