I am using following code to download a pdf file from web-
public void DownloadFile(string fileName){
var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string filePath = Path.Combine (documentsPath, fileName);
using (var writer = File.OpenWrite(filePath))
{
var restClient = new RestClient(RecepieUrl);
var request = new RestRequest(Method.GET);
request.ResponseWriter = (responseStream) => responseStream.CopyTo(writer);
var responseData = restClient.DownloadData(request);
}
}
I then try reading the file using depenedency service as follows-
public void OpenFile (string fileName)
{
//get the intent
Intent intent = new Intent(Intent.ActionView);
var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string filePath = Path.Combine (documentsPath, fileName);
var bytes = File.ReadAllBytes(filePath);
//Copy the private file's data to the EXTERNAL PUBLIC location
var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + fileName;
File.WriteAllBytes(externalPath, bytes);
intent.SetDataAndType(global::Android.Net.Uri.Parse(externalPath), "application/pdf");
intent.SetFlags(ActivityFlags.NewTask);
try {
var context = Forms.Context;
context.StartActivity(intent);
} catch (Exception e) {
string s = e.Message;
}
}
But I keep getting following exception-
System.UnauthorizedAccessException: Access to the path "/storage/emulated/074.pdf" is denied.
at at System.IO.FileStream..ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,bool,System.IO.FileOptions)
at at System.IO.FileStream..ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
at at System.IO.File.Create (string,int)
at at System.IO.File.Create (string)
at at System.IO.File.WriteAllBytes (string,byte[])
I have assigned WRITE_EXTERNAL_STORAGE in my manifest file.
Thanks