Issue
I'm getting path to save a video .mp4 file by using the following code in PCL project, which return "/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
I've added permissions to Android.Manifest as bellow:
<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" />
And this function will get the .mp4 file from internet and save to the path:
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("&", "&");
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);
}
}
}
}
}
When the program running into "target.Write(buffer, 0, bytes);" I get the exception "Access to the path is denied", so anyone plesae tell me why is this error happening?
I already tried Plugin.permissions:
Dictionary<Permission, PermissionStatus> results =
await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
PermissionStatus status = results[Permission.Storage];
but I got "status = Permission.Unknow"
I already tried to save the video to a private path that only my application can access following code, which return "/data/user/0/com.companyname.VideoDownloader/files/":
var dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
video.path = System.IO.Path.Combine(dir, filename);
And downloadVideo function can be able to write all the bytes data into the path with no error, but I cannot see any files when browse to that path.
Here is my SDK configuration:
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="25" />
Solution
Since your target sdk is 25, you have to request permissions at runtime. You could refer to this blog for how to do it in xamarin.
Answered By - Billy Liu - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.