在unity3d中Rerfreshing android相机卷

时间:2016-12-08 21:39:50

标签: c# android unity3d unity5

我为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 ());
}

请帮忙!

2 个答案:

答案 0 :(得分:2)

第二个工作解决方案是:

for i, el in enumerate(l):
    if '[' in el:
        month = el.split('[')[0]
    else:
        if '(' in el:
            mylist.append({'Month':month, 'Value':el.split('(')[0]})
        else:
            print("Cannot find a match for {}".format(el))

答案 1 :(得分:1)

使用MediaScannerConnection。您需要将图像&#34;扫描到&#34;画廊。

Native Java Code类似于:

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

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