Xamarin 调用DownloadManager下载文件提示:Java.Lang.IllegalArgumentException: Not a file URI: /storage/emulated/0/Download/2.doc

private void StartDownload()
{
        string fileUrl = "http://www.xxx.com/doc/2.doc";

        DownloadManager m_DownloadManager = (DownloadManager)GetSystemService( Context.DownloadService); 
        DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(fileUrl));

        request.SetDestinationUri(Android.Net.Uri.Parse("file://" + GetMyAppDir() + "/2.doc")); //添加"file://"前缀解决此问题
        request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
        long requestId = m_DownloadManager.Enqueue(request);
}

public string GetMyAppDir()
{
        var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
        var dir = path.AbsolutePath + Java.IO.File.Separator + "MyAppFolder";
        if (!Directory.Exists(dir))
        {
                Directory.CreateDirectory(dir);
        }
        return dir;
}

 

posted @ 2018-01-26 14:22  ziwuxian  阅读(974)  评论(0)    收藏  举报