找不到与给定名称Xamarin Android匹配的资源

时间:2018-07-31 14:23:27

标签: android xamarin xamarin.forms xamarin.android

我正在尝试在 Xamarin.Forms 中实现文件提供程序,并且在加载文件路径文件时遇到问题。每当我尝试构建该应用程序时,都会出现以下错误:

  

未找到与给定名称匹配的资源(在“ resource”中带有   值'@ xml / file_paths')

file_paths.xml 文件位于xml文件夹下的resources目录中。任何帮助将不胜感激。这是我的相关文件:

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" package="com.mycompany.myapp" android:versionName="1" android:installLocation="internalOnly">
<uses-sdk android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/advanced" android:label="FD CANNECT" android:windowSoftInputMode="adjustPan" android:descendantFocusability="afterDescendants" >
    <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.myapp.fileprovider" 
      android:exported="false" 
      android:grantUriPermissions="true">
        <meta-data 
          android:name="android.support.FILE_PROVIDER_PATHS" 
          android:resource="@xml/file_paths" />
    </provider>
</application>
</manifest>

file_paths.xml

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="downloads" path="downloads/" />
</paths>

MainActivity.cs

using Android.Provider;
using Android.Support.V4.Content;
using CAN2LAN.Droid;
using Java.Lang;

[assembly: Xamarin.Forms.Dependency(typeof(ShareIntent))]

namespace CAN2LAN.Droid
{
using Acr.UserDialogs;
using System.Linq;
using Xamarin.Forms;

using Xamarin.Forms.Platform.Android;

global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        UserDialogs.Init(this);
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);

        Downloaded();

        Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) => {
            args.Handled = false;
        };

        this.LoadApplication(new App());
    }

    public void Downloaded() {
        CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file => {
            string fileName = Android.Net.Uri.Parse(file.Url).Path.Split('/').Last();
            return System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads).AbsolutePath, fileName);
        });
    }


}

public class ShareIntent : IShareable {
    public void OpenShareIntent(string fileName) {         
        OpenFile(fileName);
    }

    public void OpenFile(string fileName) {
        string auth = "com.myapp.fileprovider";
        System.Diagnostics.Debug.WriteLine("Auth is " + auth);
        string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(fileName.ToLower()));
        if (mimeType == null)
            mimeType = "*/*";
        Android.Net.Uri uri = null;
        System.Diagnostics.Debug.WriteLine("Created authentication and mime type");
        var file = new Java.IO.File(System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads).AbsolutePath, fileName));
        System.Diagnostics.Debug.WriteLine("File size is " + file.TotalSpace);
        System.Diagnostics.Debug.WriteLine("Froms context is " + Forms.Context);
        try {
             uri = FileProvider.GetUriForFile(Forms.Context, auth, file);
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception " + e);
        }

        System.Diagnostics.Debug.WriteLine("URI is "  + uri);


        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(uri, mimeType);
        intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
        intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.NoHistory);

        System.Diagnostics.Debug.WriteLine("Intent is created");
        // Trying to allow writing to the external app ...
        var resInfoList = Forms.Context.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
        foreach (var resolveInfo in resInfoList) {
            var packageName = resolveInfo.ActivityInfo.PackageName;
            Forms.Context.GrantUriPermission(packageName, uri, ActivityFlags.GrantWriteUriPermission | ActivityFlags.GrantPrefixUriPermission | ActivityFlags.GrantReadUriPermission);
        }
        System.Diagnostics.Debug.WriteLine("We have created res info lsit");
        try
        {
            System.Diagnostics.Debug.WriteLine("We are about to show the intent");
            Forms.Context.StartActivity(intent);
            System.Diagnostics.Debug.WriteLine("We ahe shwoed the intent");

        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("We have caugth an exception " + e);
        }


    }
}
}

3 个答案:

答案 0 :(得分:2)

我没有尝试过,但这可能是问题所在。 您的 file_paths.xml 文件似乎出现问题。您输入的名称和路径都相同

name="downloads" path="downloads/"

可能有所不同

要么

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="external_files"/>
</paths>

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    <files-path name="files" path="."/>
</paths>

有关更多信息,您可以访问this

编辑1

检查 file_paths.xml 文件构建操作,应为 AndroidResource

答案 1 :(得分:2)

我相信@bradykey提到了这个问题的真正答案

“将构建操作更改为AndroidResource”

您可以通过右键单击文件并选择属性来执行此操作。在这里,您可以选择构建操作。

答案 2 :(得分:0)

确保您的 XML 文件全部为小写。那为我修好了。