问题描述
我试图从特定目录(文件夹)中删除文件,但它对我不起作用
I tried to delete file from specific directory(Folder) but it doesn't work for me
这是我的代码
DeletePhoto.Clicked += async (sender, args) =>
{
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
});
if ( file != null)
{
if (File.Exists(file.Path))
{
DependencyService.Get<IFileManager>().DeleteFile(file.Path);
}
file.Dispose();
}
};
在安卓中
public class FileManager : IFileManager
{
public void DeleteFile(string source)
{
File.Delete(source);
}
}
IFileManager 接口
IFileManager Interface
public interface IFileManager
{
void DeleteFile(string source);
}
在 mainfest 中给出了许可,但没有发生什么,我找到了我删除的文件
In mainfest permisson is given but something didn't happen and i find the file which i deleted
推荐答案
问题是由PickPhotoAsync()
方法引起的,这个方法是在blow目录下保存了一张图片的COPY:>
The problem is caused by PickPhotoAsync()
method, this method is saving a COPY of the image in the blow directory:
"/storage/emulated/0/Android/data/{package name}/files/Pictures/temp/***.jpg"
你可以调试找到这个,你可以通过this
you can debug to find this and you can check this issue of MediaPlugin by this
所以当您使用PickPhotoAsync()
方法选择文件时,您删除的实际文件是temp
目录中的副本.
If you want to delete the original file, you will have to change the parameter of
DeleteFile(string source)
方法到原路径如下:
string path= "/storage/emulated/0/Android/data/{package name}/files/Pictures/Test/***.jpg";
DeleteFile(path);
这篇关于如何使用 xamarin 表单删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!