HoloLens开发学习------应用程序中相机拍摄照片保存到自定义文件夹

项目用的是:    unity 2018.4.9    vs2017    HoloToolkit  2017.4.2.0

1.  目前在做项目中路径解决的办法:

       利用拼接方法  直接保存到yu文件夹.但是没有权限 .

       文件就是一个路径, 利用文件移动的方法保存
                    System.IO.File.Move(currentImagePath, System.IO.Path.Combine(pictureFolderPath, "yu", System.IO.Path.GetFileName(currentImagePath)));

 public void TakePhoto()
    {
        if (isReady)
        {
            string file = string.Format(@"Image_{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now);
           //利用拼接方法  直接保存到yu文件夹.但是没有权限 
           //string strtemp = System.IO.Path.Combine(Application.persistentDataPath, @"\yu");
            currentImagePath = System.IO.Path.Combine(Application.persistentDataPath, file);
            capture.TakePhotoAsync(currentImagePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
        }
        else
        {
            Debug.LogWarning("照相机还没有准备好。。。。。");
        }
    }

 private void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result)
    {
        if (result.success)
        {

#if NETFX_CORE
            try 
            {
                if(pictureFolderPath != null)
                {
                   //利用文件移动的方法保存
                    System.IO.File.Move(currentImagePath, System.IO.Path.Combine(pictureFolderPath, "yu", System.IO.Path.GetFileName(currentImagePath)));
                    Info.text = "保存图片到相机yu 中";
                }
                else 
                {
                    Info.text = "临时保存照片";
                }
            } 
            catch(Exception e) 
            {
                Info.text = "保存图片到相机camera  roll 中失败";
                Debug.Log(e.Message);
            }
#else
            Info.text = "图片已经保存";
            Debug.Log("保存图片到 " + currentImagePath);
#endif

        }
        else
        {
            Info.text = "Failed to save photo";
            Debug.LogError(string.Format("Failed to save photo to disk ({0})", result.hResult));
        }
    }

2.有关System.IO.File.Move(,)方法     官网模板如下:

C#中 try catch

       ①、代码放到try快中(try是c#的关键字)。代码运行是,会尝试执行try块内部的语句,如果么有语句发生异常,这些语句将顺序执行下去。直到全部都完成,但是一旦出现异常就跳出try块,执行catch块中的内容。

      ②、try块需要一个或者多个catch块程序捕捉并处理特定类型的异常。

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = @"c:\temp2\MyTest.txt";
        try 
        {
            if (!File.Exists(path)) 
            {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                using (FileStream fs = File.Create(path)) {}
            }

            // Ensure that the target does not exist.
            if (File.Exists(path2))	
            File.Delete(path2);

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved to {1}.", path, path2);

            // See if the original exists now.
            if (File.Exists(path)) 
            {
                Console.WriteLine("The original file still exists, which is unexpected.");
            } 
            else 
            {
                Console.WriteLine("The original file no longer exists, which is expected.");
            }			

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

 

3.有关C#的路径拼接问题

             C#将多个字符串组合成一个路径,可利用System.IO.Path类中的Combine(String,String)方法

        命名空间为:using System.IO;



        string path1 = @"D:\temp";
        string path2 = "result.txt";
        string newPath = Path.Combine(path1, path2); 
        // newPath = "D:\temp\result.txt";
        //path.Combine方法拥有多个重载



//返回上一层路径的方法:
        string path="c:\admin\demo";
        Directory.SetCurrentDirectory(Directory.GetParent(path).FullName);
        path = Directory.GetCurrentDirectory(); //path="c:\admin";
        //返回几层则可以使用for循环

常用的路径:
            //获取当前程序路径,包括文件名
            string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            //获取应用程序的当前工作目录。 
            string str3 = System.IO.Directory.GetCurrentDirectory();
            string str4 = AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。 
            string str5 = System.Windows.Forms.Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。 
            string str6 = System.Windows.Forms.Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。 
            string str7 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
            System.Environment.GetEnvironmentVariable("windir"); //=C:\WINDOWS

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值