首页 > 解决方案 > 如何在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。我已经提供了写作权限。

标签: c#androidandroid-activitydownload

解决方案


您可以将外部存储上的公共文件的存储目录用作PUBLIC_EXTERNAL_STORAGE. https://docs.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows

该代码可以获得您想要的下载文件夹。

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

在此处输入图像描述


推荐阅读