如何解决使用 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?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。