【问题标题】:Rerfreshing android camera roll in unity3d在unity3d中重新刷新android相机胶卷
【发布时间】:2016-12-08 21:39:49
【问题描述】:

我为Android设备制作了一个简单的应用程序,它使用手机的相机拍照并将其保存在内部存储文件夹/mnt/sdcard/DCIM/Camerizeman/中

照片已正确保存,但我面临的问题是我无法从手机图库中看到照片。如果我使用文件管理器或重新启动设备,我可以正确看到它们。我现在正在搜索 10 天,问题是我必须在保存图像后刷新图库。

我没有找到任何可行的解决方案。

我的代码如下:

RenderTexture renderTex;
Texture2D screenshot;
Texture2D LoadScreenshot;
int width = Screen.width; // for Taking Picture
int height = Screen.height; // for Taking Picture
string fileName;
string myScreenshotLocation;
string screenShotName = "MyImage_AR_" + System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png";
public void Snapshot ()
{
StartCoroutine (CaptureScreen ());
}
public IEnumerator CaptureScreen ()
{
yield return null; // Wait till the last possible moment before screen rendering to hide the UI
//GameObject.FindGameObjectWithTag("Snapshoot").SetActive(false);
yield return new WaitForEndOfFrame (); // Wait for screen rendering to complete
if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
mainCamera = Camera.main.GetComponent<Camera> (); // for Taking Picture
renderTex = new RenderTexture (width, height, 24);
mainCamera.targetTexture = renderTex;
RenderTexture.active = renderTex;
mainCamera.Render ();
screenshot = new Texture2D (width, height, TextureFormat.RGB24, false);
screenshot.ReadPixels (new Rect (0, 0, width, height), 0, 0);
screenshot.Apply (); //false
RenderTexture.active = null;
mainCamera.targetTexture = null;
}
if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight) {
mainCamera = Camera.main.GetComponent<Camera> (); // for Taking Picture
renderTex = new RenderTexture (height, width, 24);
mainCamera.targetTexture = renderTex;
RenderTexture.active = renderTex;
mainCamera.Render ();
screenshot = new Texture2D (height, width, TextureFormat.RGB24, false);
screenshot.ReadPixels (new Rect (0, 0, height, width), 0, 0);
screenshot.Apply (); //false
RenderTexture.active = null;
mainCamera.targetTexture = null;
}
myScreenshotLocation = myFolderLocation + screenShotName;
File.WriteAllBytes (myFolderLocation + screenShotName, screenshot.EncodeToPNG ());
}

请帮忙!

【问题讨论】:

    标签: c# android unity3d unity5


    【解决方案1】:

    第二种可行的解决方案是:

    using (AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            using (AndroidJavaObject joActivity = jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            using (AndroidJavaObject joContext = joActivity.Call<AndroidJavaObject>("getApplicationContext"))
            using (AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))
            using (AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"))
            using (AndroidJavaObject joExDir = jcEnvironment.CallStatic<AndroidJavaObject>("getExternalStorageDirectory"))
            {
                jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { YOURFULL IMAGE PATH}, null, null);
            }
    

    【讨论】:

    • 我正在发布我的答案。没关系。 +1
    【解决方案2】:

    使用MediaScannerConnection。您需要将图像“扫描到”图库中。

    本机 Java 代码类似于:

    MediaScannerConnection.scanFile(unityPlayerActivity, new String[]{externalImagePath}, null, null);
    

    可以创建插件 - Unity Android plugin tutorial (1/3) Fundamentals -,或使用 AndroidJavaClass.CallStatic 调用 MediaScannerConnection.scanFile

    【讨论】:

    • 当然@Geri...非常感谢您..我会感谢您的帮助!
    • 你能在这里创建一个私人聊天室,以便我们可以和 Geri 交谈吗?
    • .. :-( 我需要 20 声望才能聊天...有任何电子邮件可以向您发送我的 fb acc 吗?
    • 简化答案,使用谷歌了解详情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多