本文主要是介绍截屏保存Android相册,并刷新相册,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接上代码:
1、截屏文件:
public IEnumerator SaveScreenCoroutine (){string imageName = "DrawingAndColoring-"+System.DateTime.Now.ToString ("yyyy-MM-dd_HH-mm-ss");//Capture screen shotyield return new WaitForEndOfFrame();Texture2D texture = new Texture2D(Screen.width, Screen.height);texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);byte[] bytes = texture.EncodeToPNG();texture.Apply();yield return texture;if (Application.platform == RuntimePlatform.Android) {string pathSave;string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));string destination = path + "/Pictures/";if (!Directory.Exists(destination)) {Directory.CreateDirectory(destination);}pathSave = destination +"/"+ imageName + ".png";//保存文件File.WriteAllBytes(pathSave, bytes);//刷新文件string[] paths = new string[1];paths[0] = pathSave;ScanFile(paths);}}
2、刷新文件:
//刷新图片,显示到相册中void ScanFile(string[] path) {using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null)) {Conn.CallStatic("scanFile", playerActivity, path, null, null);}}}
测试:直接调用方法SaveScreenCoroutine
这篇关于截屏保存Android相册,并刷新相册的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!