Android 表单将文件(pdf)保存在本地存储中,并使用默认查看器打开
编辑以细化问题 在Android中,我需要下载一个pdf文件到“Documents”文件夹,并用默认程序打开它。 我下载了文档并将其存储在字节数组中。 然后我把这个方法叫做:Android 表单将文件(pdf)保存在本地存储中,并使用默认查看器打开,android,xamarin,xamarin.forms,launching-application,android-storage,Android,Xamarin,Xamarin.forms,Launching Application,Android Storage,编辑以细化问题 在Android中,我需要下载一个pdf文件到“Documents”文件夹,并用默认程序打开它。 我下载了文档并将其存储在字节数组中。 然后我把这个方法叫做: /// <summary> /// Save data bytes on "My documents" folder and open /// </summary> /// <param name="fileName">Filename, for exam
/// <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;
}
}
//
///在“我的文档”文件夹中保存数据字节并打开
///
///文件名,例如:“Test.pdf”
///要保存的字节数组
///成功时为True,错误时为false
public bool SaveAndOpen(字符串文件名,字节[]数据)
{
尝试
{
//获取我的文档路径
string filePath=Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath,Android.OS.Environment.DirectoryDownloads);
//结合文件名
字符串fullName=Path.Combine(文件路径,文件名);
//如果设备上存在该文件,请将其删除
if(File.Exists(全名))
{
//注意:在该方法的第二次运行中,该文件存在
文件。删除(全名);
}
//在“我的文档”中写入字节
File.writealBytes(全名、数据);
//作为进程启动文件
Process.Start(全名);
返回true;
}
捕获(例外情况除外)
{
Console.WriteLine(“错误:+ex.Message+ex.StackTrace”);
返回false;
}
}
问题是使用默认的pdf阅读器打开pdf文档
行“Process.Start(fullName);”引发异常
System.ComponentModel.Win32异常(0x80004005):访问被拒绝
谢谢你的时间 我找到了保存和打开文件的方法:
/// <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 downloads 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);
// Get the uri for the saved file
Android.Net.Uri file = Android.Support.V4.Content.FileProvider.GetUriForFile(
this,
this.PackageName + ".fileprovider",
new Java.IO.File(fileName));
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(file, "application/pdf");
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask | ActivityFlags.GrantReadUriPermission | ActivityFlags.NewTask);
this.ApplicationContext.StartActivity(intent);
return true;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message + ex.StackTrace);
return false;
}
}
还要在AndroidManifest.xml文件中添加提供程序:
<application android:label="My app" android:icon="@mipmap/icon">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
在同一个AndroidManifest中,添加以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
并在Resources/xml/file_paths.xml中添加一个外部路径
<external-path name="external_files" path="."/>
不知道我是否不需要权限,但这种方式有效。我找到了保存和打开文件的方法:
/// <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 downloads 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);
// Get the uri for the saved file
Android.Net.Uri file = Android.Support.V4.Content.FileProvider.GetUriForFile(
this,
this.PackageName + ".fileprovider",
new Java.IO.File(fileName));
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(file, "application/pdf");
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask | ActivityFlags.GrantReadUriPermission | ActivityFlags.NewTask);
this.ApplicationContext.StartActivity(intent);
return true;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message + ex.StackTrace);
return false;
}
}
还要在AndroidManifest.xml文件中添加提供程序:
<application android:label="My app" android:icon="@mipmap/icon">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
在同一个AndroidManifest中,添加以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
并在Resources/xml/file_paths.xml中添加一个外部路径
<external-path name="external_files" path="."/>
不知道我是否不需要权限,但这样做是可行的。
string filePath=System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)代码>Wel。。。那是哪条路?请输入完整路径值。文件路径为:/data/user/0/MySampleApp.MySampleApp/files全名为:/data/user/0/MySampleApp.MySampleApp/files/Test.pdf仅位于应用程序的专用目录中。没有其他应用程序或文件管理器具有访问权限。如果希望文件管理器具有访问权限,请为下载选择另一个存储位置。/storage/emulated/0/Documents
将是路径。但是不要硬编码。你实现了向用户询问运行时权限吗在Android中,
哪个版本?字符串文件路径=System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)代码>Wel。。。那是哪条路?请输入完整路径值。文件路径为:/data/user/0/MySampleApp.MySampleApp/files全名为:/data/user/0/MySampleApp.MySampleApp/files/Test.pdf仅位于应用程序的专用目录中。没有其他应用程序或文件管理器具有访问权限。如果希望文件管理器具有访问权限,请为下载选择另一个存储位置。/storage/emulated/0/Documents
将是路径。但是不要硬编码。你实现了向用户询问运行时权限吗在Android中,
哪个版本?经过6个小时的浏览,我终于找到了这个答案,它解决了我的问题,谢谢@Duefectuany iOS版本?经过6个小时的冲浪,我终于找到了这个答案,它解决了我的问题,谢谢@这有什么iOS版本?