程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 xamarin 插件扩展创建文件夹目录以保存文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 xamarin 插件扩展创建文件夹目录以保存文件?

开发过程中遇到使用 xamarin 插件扩展创建文件夹目录以保存文件的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 xamarin 插件扩展创建文件夹目录以保存文件的解决方法建议,希望对你解决使用 xamarin 插件扩展创建文件夹目录以保存文件有所启发或帮助;

我想创建一个文件夹目录,并在该文件夹中保存图像并获得响应。但是当我使用文件资源管理器手动检查时,文件夹没有显示。

//拍照代码

String Dirname = "Sample";
String imgname = "image.jpg";
String basepath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);

 takePhoto.Clicked += async (sender,args) =>
        {

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await displayAlert("No Camera",":( No camera available.","OK");
                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,});

            byte[] imageArray = null;
            if (file != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    var stream = file.GetStream();
                    stream.copyTo(ms);
                    imageArray = ms.ToArray();
                }
            }
            Stream data = new MemoryStream(imageArray);

            if (file == null)
                return;
            
            filePath = file.Path;
            paths.Enqueue(filePath);
            var result = await CrossEDFstemplate.Current.Savefile(basepath,Dirname,imgname,filePath);
            await displayAlert("succesful",result.ToString(),"ok");

//目录创建代码

    public async Task<SavefileResponse> Savefile(String FolderBasePath,String Foldername,String 
    filename,String fileFullPath = null,Stream data = null)
    {
        SaveCompletionsource = new taskcompletionsource<SavefileResponse>();
        if (FolderBasePath != null && Foldername != null)
        {
            var directoryPath = Path.Combine(FolderBasePath,FolderName);
            String NemfilePath = Path.Combine(directoryPath,fileName);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            if (fileFullPath != null)
            {
                var imageData = file.ReadAllBytes(fileFullPath);

                file.WriteallBytes(NemfilePath,imageData);

            }
            else if (data != null)
            {
                byte[] barray = new byte[data.Length];
                using (fileStream fs = new fileStream(NemfilePath,fileMode.openorcreate))
                {
                    using (data)
                    {
                        data.Read(barray,(int)data.Length);
                    }
                    int length = barray.Length;
                    fs.Write(barray,length);
                }
            }
            else
            {
                var ResponseSaved = new SavefileResponse("There are no items to Save",null,fileName);
                SavefileError(this,ResponseSaved);
                SaveCompletionsource.TrySetResult(ResponseSaved);
            }
        }
        else
        {
            return await SaveCompletionsource.Task;
        }
        return await SaveCompletionsource.Task;
    }

根据此代码,该目录正在创建,但是当我使用文件资源管理器手动检查该文件夹时,该文件夹未显示。

解决方法

您用来保存文件的路径 Environment.SpecialFolder.MyPictures 是内部存储。

在内部存储中,没有 root 权限您无法查看文件。

但是您可以使用代码来检查文件是否存在于内部存储中。

 if (File.Exists(filepath))
    {

    }

如果你想查看它,你可以使用 adb 工具。请检查链接中的方式。

How to write the username in a local txt file when login success and check on file for next login?

大佬总结

以上是大佬教程为你收集整理的使用 xamarin 插件扩展创建文件夹目录以保存文件全部内容,希望文章能够帮你解决使用 xamarin 插件扩展创建文件夹目录以保存文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。