我有一个Xamarin应用程序,我想用该类型的默认应用程序(doc,pdf,txt,jpg,jpg等)打开附加到数据行的文件,所以当用户点击包含名称的ListItem时我要打开该文件的附件文件。
我正在测试它的Android部分以及何时到达
FileProvider.GetUriForFile(blablabla see below
它崩溃:
尝试在空对象引用上调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)
我从应用程序服务器获取附件,并将其写入
Environment.SpecialFolder.Personal
我检查了它是否真的写在那里,并且在那里。
代码从这里:https://forums.xamarin.com/discussion/124361/unable-to-open-pdf-into-the-third-party-app
它是:
public void OpenFileByName(string filenameWithPath)
{
try
{
string application = "";
string filename = Path.GetFileName(filenameWithPath);
string extension = Path.GetExtension(filename);
// get mimeType
if (extension != null)
switch (extension.ToLower())
{
case ".txt":
application = "text/plain";
break;
case ".doc":
case ".docx":
application = "application/msword";
break;
case ".pdf":
application = "application/pdf";
break;
case ".xls":
case ".xlsx":
application = "application/vnd.ms-excel";
break;
case ".jpg":
case ".jpeg":
case ".png":
application = "image/jpeg";
break;
default:
application = "*/*";
break;
}
Java.IO.File file = new Java.IO.File(filenameWithPath);
file.SetReadable(true);
Android.Net.Uri uri = Android.Support.V4.Content.FileProvider.GetUriForFile(Android.App.Application.Context, "com.example.asd.fileprovider", file);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, application);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.NoHistory);
intent.AddFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try
{
Android.App.Application.Context.StartActivity(intent);
}
catch (Exception)
{
Toast.MakeText(Android.App.Application.Context, "No Application Available to View this file.", ToastLength.Short).Show();
}
}
catch (Exception ex)
{
Toast.MakeText(Android.App.Application.Context, ex.Message, ToastLength.Short).Show();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.example.asd"
android:installLocation="auto">
<provider
android:name="android.support.v4.content.fileProvider"
android:authorities="com.example.asd.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
Android / Resource / xml / filepaths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<files-path name="media"/>
<files-path name="images"/>
<files-path name="docs"/>
<files-path name="download"/>
</paths>
Android / Resources / drawable / file_provider_path.xml:
<?xml version="1.0" encoding="utf-8" ?>
<paths>
<external-path name="download" path="download/"/>
</paths>
这是怎么了?
答案 0 :(得分:0)
要解决此错误,请将<provider>
元素放在应用清单的<application>
元素中,如图here所示。
我不确定file_provider_path.xml
的用途是什么,但是它不应该位于drawable
目录中,因为它不是Android可绘制对象。