如何在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_STORAGEhttps://docs.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows

代码可以获取所需的下载文件夹。

var DIR = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);

enter image description here