Unity Android Android and iOS save pictures and display them in the album

Unity Android Android and iOS save pictures and display them in the album

1. Android

First in PlayerSettings- othersettings-writeacess , change to ExternalSDcard

The Android phone may have an SD card or not. If there is an SD card, directly use Unity’s screenshot API or Application path will be saved at the same time.

The following is written inside the phone.


Resource path corresponding to android platform

Application.dataPath    /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath    jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath    /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath    /data/data/xxx.xxx.xxx/cache
 

//Storage path destination picture name screenshotFilename //PhotoCam camera RenderTexture currentRT = RenderTexture.active; RenderTexture.active = PhotoCam.targetTexture; PhotoCam.Render(); Texture2D image = new Texture2D((int)Screen.width, (int)Screen.height); image.ReadPixels(new Rect(0, 0, (int)Screen.width, (int)Screen.height), 0, 0); image.Apply(); RenderTexture.active = currentRT; byte[] bytes = image.EncodeToPNG(); string destination = "/storage/emulated/0/DCIM/ARPhoto"; //Judging whether the directory exists, it will create the directory if it does not exist if (!Directory.Exists (destination)) { Directory.CreateDirectory (destination); } string Path_save = destination+"/" + screenshotFilename; //Save picture System.IO.File.WriteAllBytes(Path_save, bytes); The picture is saved above, but it will not appear in the album. Let the picture appear in the album below (update) //**************Method 1 Screenshot, save path*****************// Debug.Log("Android platform detected"); string androidPath = "/storage/emulated/0/DCIM/ARPhoto" + "/" + screenshotFilename; photoPath = androidPath; string path = androidPath; string pathonly = Path.GetDirectoryName(path); Directory.CreateDirectory(pathonly); AndroidJavaClass obj = new AndroidJavaClass("com.ryanwebb.androidscreenshot.MainActivity"); while(!photoSaved) { photoSaved = obj.CallStatic<bool>("scanMedia", path); yield return new WaitForSeconds(.1f); } //**********************************************//

 

 

 

Hahaha forgot to give the jar

Link: https://download.csdn.net/download/u014528558/10673575

 

 

 

2.iOS 

Reference http://blog.csdn.net/wf_unity/article/details/22087643

 

 

3.unity

Resource path in Unity3D
Application.dataPath This property is used to return the path of the folder where the data file of the program is located. For example, it is Assets in Editor.
Application.streamingAssetsPath This property is used to return the cache directory of streaming data. The return path is a relative path, which is suitable for setting the path of some external data files.
Application.persistentDataPath This property is used to return the path of a persistent data storage directory, under which some persistent data files can be stored.
Application.temporaryCachePath This property is used to return a temporary data cache directory.


Reference : https://blog.csdn.net/u014528558/article/details/54691735