c# - 如何在xamarin的mainactivity中更改android下载文件的路径?
问题描述
我添加了下载功能,它工作正常,但下载的文件存储在storage/emulated/0/Application/data/file/Download
文件夹中。但我想更改下载路径并想在storage/emulated/0/Download
文件夹中下载。我的代码如下。
public void Downloaded()
{
CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func < IDownloadFile, string > (file = > {
string fileName = Android.Net.Uri.Parse(file.Url).Path.Split('/').Last();
return Path.Combine(ApplicationContext.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath, fileName);
});
}
如果我添加静态路径进行下载,那么它会显示当前进程的权限错误android.permission.WRITE_EXTERNAL_STORAGE
。我已经提供了写作权限。
解决方案
您可以将外部存储上的公共文件的存储目录用作PUBLIC_EXTERNAL_STORAGE
. https://docs.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows
该代码可以获得您想要的下载文件夹。
var DIR = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
推荐阅读
- sql - 当窗口函数遇到空值时,Redshift 列别名不起作用?还有谁?
- javascript - 在 UI 上排序数据和渲染不使用反应钩子
- android - 如何在 json 反序列化期间从列表中删除某些元素?
- java - 使用签名链接的 S3 大文件上传
- docker - Windows 10 docker-compose 占用 100% 磁盘使用率
- powershell - PowerShell 将托管属性映射到已爬网属性
- cordova - 使用 Cordova CLI 3.6 安装 Cordova 最新版本?
- c# - 如何在 Xamrin Android 应用程序中使用 ASMX soap?
- matlab - 使用 radiomics 包的输入参数不足
- stream - 创建自定义标准输出流