您的位置:首页 > 移动开发 > Unity3D

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

2017-01-23 10:26 811 查看
1.安卓

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

下面写存在手机内部。

//存储路径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了

链接:http://pan.baidu.com/s/1gfggIav 密码:3nld

2.iOS 

参考http://blog.csdn.net/wf_unity/article/details/22087643
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: