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了

链接:Unity安卓手机图片显示在相册中更新-Android文档类资源-CSDN下载

2.iOS 

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

3.unity

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

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值