Unity安卓Android与iOS保存图片并显示在相册

1.安卓

先在PlayerSettings-othersettings-writeacess,改为ExternalSDcard

安卓手机有可能有SD卡 有可能没,如果有SD卡,直接用unity的截屏API或者Application的路径会同时保存,

下面写存在手机内部。


android平台对应的资源路径

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
 

 //存储路径destination 图片名screenshotFilename
	    //PhotoCam摄像头
	    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";
            //判断目录是否存在,不存在则会创建目录
            if (!Directory.Exists (destination)) {
                Directory.CreateDirectory (destination);
            }
            string Path_save = destination+"/" + screenshotFilename;
            //存图片
            System.IO.File.WriteAllBytes(Path_save, bytes);  

上面完成了图片的保存 但是不会出现在相册,下面让图片出现在相册(更新)
	    //**************方法1 屏幕截图,保存路径*****************//
            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);
            }
            //**********************************************//    

哈哈哈 忘记给jar了

链接:https://download.csdn.net/download/u014528558/10673575

2.iOS 

参考http://blog.csdn.net/wf_unity/article/details/22087643

3.unity

Unity3D中的资源路径
Application.dataPath    此属性用于返回程序的数据文件所在文件夹的路径。例如在Editor中就是Assets了。
Application.streamingAssetsPath    此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。
Application.persistentDataPath    此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。
Application.temporaryCachePath    此属性用于返回一个临时数据的缓存目录。

PA_
发布了29 篇原创文章 · 获赞 15 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/u014528558/article/details/54691735