Xamarin Android - 写入文件 - 拒绝访问

时间:2018-05-29 09:24:01

标签: xamarin xamarin.forms xamarin.ios xamarin.android

我在 PCL 项目中使用以下代码获取保存视频.mp4文件的路径,该项目返回" / storage / emulated / 0 / DCIM / ":

 #if __ANDROID__
   var dir = Android.OS.Environment
   .GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
   var publicPath= dir.AbsolutePath;
   var newFilepath = System.IO.Path.Combine(publicPath, fileName); 
   video.path = newFilepath;
 #endif

我已将 Android.Manifest 的权限添加为:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

此功能将从互联网获取.mp4文件并保存到路径:

 public void downloadVideo(VideoInfor video, bool isRetried)
    {
        if (!video.downloadUrl.Contains(HLS) && !video.downloadUrl.Contains(HLS_1))
        {
            string filepath = video.path;

            if (Uri.IsWellFormedUriString(video.downloadUrl, UriKind.RelativeOrAbsolute) && !video.downloadUrl.Contains("https://v.vnecdn.net/vnexpress/video/video_default.mp4"))
            {
                video.downloadUrl = video.downloadUrl.Replace("&amp;", "&");
                Console.WriteLine("Downloading url:  " + video.downloadUrl);
                try
                {
                    if (!video.downloadUrl.Contains(".m3u8"))
                    {
                        Console.WriteLine("Downloading.....");

                        var request = (HttpWebRequest)WebRequest.Create(video.downloadUrl);
                        if (isRetried)
                        {
                            request.Timeout = 120000;
                        }
                        using (WebResponse response = request.GetResponse())
                        {
                            using (Stream source = response.GetResponseStream())
                            {
                                using (FileStream target = File.Open(filepath, FileMode.Create, System.IO.FileAccess.Write))
                                {
                                    var buffer = new byte[1024];
                                    bool cancel = false;
                                    int bytes;
                                    int copiedBytes = 0;

                                    while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        target.Write(buffer, 0, bytes);

                                        copiedBytes += bytes;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception download url = " + video.downloadUrl);
                    Console.WriteLine("Exception download url Exception = " + ex.ToString());
                    if (!isRetried)
                    {
                        downloadVideo(video, true);
                    }

                }
            }
        }
    }

当程序运行到&#34; t arget.Write(缓冲区,0,字节); &#34;我得到例外&#34;拒绝访问路径&#34;,所以任何人都告诉我为什么会发生这个错误?

我已经尝试过 Plugin.permissions

 Dictionary<Permission, PermissionStatus> results = 
 await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
 PermissionStatus status = results[Permission.Storage];

但我得到了&#34; status = Permission.Unknow &#34;

我已经尝试将视频保存到专用路径,只有我的应用程序才能访问以下代码,这些代码返回&#34; /data/user/0/com.companyname.VideoDownloader/files / &#34;:

 var dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
     video.path = System.IO.Path.Combine(dir, filename);

并且downloadVideo函数可以将所有字节数据写入路径而没有错误,但是当浏览到该路径时我看不到任何文件。

这是我的SDK配置:

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="25" />

1 个答案:

答案 0 :(得分:0)

由于您的目标sdk为25,因此您必须request permissions at runtime。您可以参考此blog了解如何在xamarin中执行此操作。