【问题标题】:Create Folder Directory to Save file using xamarin plugin extension使用 xamarin 插件扩展创建文件夹目录以保存文件
【发布时间】:2021-05-06 05:22:57
【问题描述】:

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

//拍照代码

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, 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;
    }

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

【问题讨论】:

  • 您不能只使用 Windows 文件资源管理器浏览 Android 文件系统。您需要使用 adb 工具

标签: c# xamarin xamarin.forms plugins xamarin.android


【解决方案1】:

你用来保存文件的路径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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多