【问题标题】:FileProvider.GetUriForFile crashes with null reference exception in Xamarin-AndroidFileProvider.GetUriForFile 在 Xamarin-Android 中因空引用异常而崩溃
【发布时间】:2019-03-22 10:26:34
【问题描述】:

我有一个 Xamarin 应用程序,我想在其中使用该类型的默认应用程序(doc、pdf、txt、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>

这里有什么问题?

【问题讨论】:

  • 关键部分是调试和找出究竟什么是空的。
  • 我调试了3天了。正如我写它在 Android.Net.Uri uri = Android.Support.V4.Content.FileProvider.GetUriForFile(Android.App.Application.Context, "com.example.asd.fileprovider", file);文件不为空,它具有正确的值。
  • 好吧,如果文件中有数据,那么剩下 3 样东西.. Android.Support.V4.Content.FileProvider, Android.App.Application.Context 和 com.example.asd.fileprovider跨度>
  • 我当然试过了。请参阅:上下文上下文 = Android.App.Application.Context; Android.Net.Uri uri = FileProvider.GetUriForFile(context, "com.example.asd.fileprovider", file);这里的上下文也有价值。我认为这可能是文件提供程序的问题,但是什么?

标签: c# android visual-studio xamarin


【解决方案1】:

要修复错误,请将&lt;provider&gt; 元素放入应用清单中的&lt;application&gt; 元素中,如here 所示。

我不确定file_provider_path.xml 的用途,但它不应该在drawable 目录中,因为它不是Android 可绘制对象。

【讨论】:

  • 谢谢,这就是问题所在。我仍然收到错误,但它与文件系统有关。
  • 我也解决了文件系统权限问题。再说一遍,@rubo 写的答案就是我的问题的解决方案!
猜你喜欢
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2019-11-29
相关资源
最近更新 更多