简体   繁体   English

使用 xamarin 插件扩展创建文件夹目录以保存文件

[英]Create Folder Directory to Save file using xamarin plugin extension

I want to create a folder directory and in that folder, I want to save the image and get the response.我想创建一个文件夹目录,在该文件夹中,我想保存图像并获得响应。 but when I check manually using file explorer the folder is not showing.但是当我使用文件资源管理器手动检查时,该文件夹没有显示。

//take picture code //拍照代码

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");

//Directory create code //目录创建代码

    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, 0, (int)data.Length);
                    }
                    int length = bArray.Length;
                    fs.Write(bArray, 0, 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;
    }

according to this code, the directory is creating but when I manually checking that folder using file explorer the folder is not showing.根据此代码,正在创建目录,但是当我使用文件资源管理器手动检查该文件夹时,该文件夹未显示。

The path Environment.SpecialFolder.MyPictures you used to save the file is internal storage.您用于保存文件的路径Environment.SpecialFolder.MyPictures是内部存储。

In Internal Storage, you couldn't see the files without root permission.在内部存储中,没有 root 权限是看不到文件的。

But you could use the code to check the file exist or not in the internal storage.但是您可以使用代码来检查文件是否存在于内部存储中。

 if (File.Exists(filepath))
    {

    }

If you want to view it, you could use adb tool.如果你想查看它,你可以使用 adb 工具。 Please check the way in link.请检查链接中的方式。

How to write the username in a local txt file when login success and check on file for next login? 登录成功时如何将用户名写入本地txt文件并检查文件以进行下次登录?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM