我尝试通过默认的pdf查看器xamarin形式打开pdf文件,但出现异常:
Android.OS.FileUriExposedException: file:///storage/emulated/0/pdfy/pdfik.pdf通过应用程序以外的地方公开 Intent.getData()
我添加了READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE权限,并且:
while (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 0);
}
while (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage}, 0);
}
这就是我要打开文件的方式:
public bool OpenFile(string filename)
{
Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(filename));
Intent intent = new Intent(Intent.ActionView);
String mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl((string)uri).ToLower());
intent.SetDataAndType(uri, mimeType);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try
{
Xamarin.Forms.Forms.Context.StartActivity(intent);
//ApplicationContext.StartActivity(intent);
return true;
}
catch (System.Exception ex)
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View this file", ToastLength.Short).Show();
//Toast.MakeText(ApplicationContext, "No Application Available to View this file", ToastLength.Short).Show();
return false;
}
}
答案 0 :(得分:0)
尝试将FileProvider用于PDF路径:
Android.Net.Uri pdfPath = FileProvider.GetUriForFile(context, "com.mydomain.fileprovider", new Java.IO.File(filePath));