【发布时间】:2020-06-09 04:19:54
【问题描述】:
已修改问题
在 Android 中,我需要将 pdf 文件下载到“Documents”文件夹并使用默认程序打开它。 我下载了文档并将其存储在一个字节数组中。 然后我调用这个方法:
/// <summary>
/// Save data bytes on "My documents" folder and open
/// </summary>
/// <param name="fileName">Filename, for example: "Test.pdf"</param>
/// <param name="data">Array of bytes to save</param>
/// <returns>True if success or false if error</returns>
public bool SaveAndOpen(string fileName, byte[] data)
{
try
{
// Get my documents path
string filePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
// Combine with filename
string fullName = Path.Combine(filePath, fileName);
// If the file exist on device delete it
if (File.Exists(fullName))
{
// Note: In the second run of this method, the file exists
File.Delete(fullName);
}
// Write bytes on "My documents"
File.WriteAllBytes(fullName, data);
// Launch file as process
Process.Start(fullName);
return true;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message + ex.StackTrace);
return false;
}
}
问题是用默认的pdf阅读器打开pdf文档。
“Process.Start(fullName);”行抛出异常
System.ComponentModel.Win32Exception (0x80004005):访问被拒绝
感谢您的宝贵时间!
【问题讨论】:
-
string filePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);嗯...那会是哪条路?请完整路径值。 -
文件路径为:/data/user/0/MySampleApp.MySampleApp/files 全名为:/data/user/0/MySampleApp.MySampleApp/files/Test.pdf
-
这只是在您的应用程序的私有目录中。没有其他应用程序或文件管理器具有访问权限。如果您希望文件管理器具有访问权限,请选择另一个存储位置进行下载。
-
/storage/emulated/0/Documents将是路径。但不要硬编码。 -
您是否实现了向用户询问运行时权限?
In Android,哪个版本?
标签: android xamarin xamarin.forms launching-application android-storage