【问题标题】:Couldn't find meta-data for provider with authority找不到具有权限的提供者的元数据
【发布时间】:2019-06-14 12:43:54
【问题描述】:

我已将 Snapchat 的 Creative Kit 集成到我的 Android 应用程序中。处理后,我从服务器接收到一个字节数组形式的图像,我将其保存到磁盘,然后将文件发送到 Snapchat 的 Creative Kit,如下所示。

 private fun downloadImage(
    fileName: String,
    imageByteArray: ByteArray?): Uri? {
    val state = Environment.getExternalStorageState()

    if (Environment.MEDIA_MOUNTED == state) {
        val downloadDir = File(
            Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name)
        )

        if (!downloadDir.isDirectory) {
            downloadDir.mkdirs()
        }

        val file = File(downloadDir, fileName)
        var ostream: FileOutputStream? = null
        try {
            ostream = FileOutputStream(file)
            ostream.write(imageByteArray)
            ostream.flush()
            ostream.close()
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }

    val snapCreativeKitApi = SnapCreative.getApi(context!!)
    val snapMediaFactory = SnapCreative.getMediaFactory(context!!)
    lateinit var snapPhotoFile: SnapPhotoFile
    try {
        snapPhotoFile = snapMediaFactory.getSnapPhotoFromFile(file)
    } catch (e: SnapMediaSizeException) {
        return
    }
    val snapPhotoContent = SnapPhotoContent(snapPhotoFile)
    snapCreativeKitApi.send(snapPhotoContent)
    }
}

我还在清单文件中添加了provider,如下所示:

  <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths_app" />
    </provider>

provider_paths_app.xml 中,我通过引用this 的答案尝试了所有可能的路径,但它们都不起作用。

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

使用上述路径,我收到以下错误。

Couldn't find meta-data for provider with authority my.package.name.fileprovider

我所要做的就是将此图像发送到 Snapchat,但我无法弄清楚我做错了什么。任何帮助将不胜感激。

【问题讨论】:

  • &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;paths &gt; &lt;files-path name="myapp" path="images/"/&gt; &lt;/paths&gt; .卸载旧的。
  • @IntelliJAmiya 现在,出现此错误:无法找到包含 /storage/emulated/0/My App Name/image.jpg 的已配置根目录
  • 我在我的每个构建类型文件夹(开发、暂存和发布)上使用了不同的清单,以便我可以在我的提供程序上设置不同的权限。使用 ${applicationId} 变量让我可以再次将清单文件统一为一个。不错:)

标签: android android-fileprovider snapchat


【解决方案1】:

首先,在&lt;application&gt;标签下的manifest中写入如下标签

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

然后在res创建一个xml文件夹,并创建一个文件名为:provider_paths.xml 然后复制粘贴代码:

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

现在大多数开发人员都会犯错误:在您的脚本中,使用以下命令创建文件:

FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
                    BuildConfig.APPLICATION_ID + ".provider", file);

【讨论】:

  • 很好解释
  • 谢谢你。我尝试了所有其他解决方案,但 BuildConfig.APPLICATION_ID + ".provider" 是它最终对我有用的原因。
  • 对我来说,问题是区分大小写。我在一个地方拥有.FileProvider,在另一个地方拥有.fileprovider。大小写必须匹配!
  • 我试图 BuildConfig.APPLICATION_ID + ".provider" 是它最终为我工作的原因
  • 为什么这么复杂
【解决方案2】:
    <provider
        android:name="androidx.core.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>

这是我的provider声明,${applicationId}的值为“com.limxtop.research”,请确保authorities的名称与下面代码的名称相同。

            // Create the file where the photo should save.
            File file = null;
            try {
                file = createImageFile();
            } catch (IOException e) {
                break;
            }
            // The second parameter is the name of authorities.
            Uri uri = FileProvider.getUriForFile(this,
                    "com.limxtop.research.fileprovider", file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            startActivityForResult(intent, fullSizeRequestCode);

所以,也许您在此处发布的代码不完整,您应该将“my.package.name.fileprovider”作为参数传递给某个地方。

【讨论】:

    【解决方案3】:

    就我而言。

    我有一个库项目。我们称之为:LibApk

    当我编写此代码时:build.grale(app) 中的 applicationId "my.package.name"

    构建 gradle 时告诉我:库项目无法设置 applicationId。

    applicationId 在默认配置中设置为“com.darcy.apkupdate”。

    所以,我删除了 applicationId "my.package.name" 。

    然后,build.gradle 看起来像这样:

    但我忘了更新使用 ${applicationId}

    的 AndroidManifest.xml 文件

    这就是问题!!!

    所以我将变量更改为常量。结果如下图所示:

    在这之后,这对我来说很重要!

    希望对你有所帮助...

    【讨论】:

      【解决方案4】:

      如果构建的后缀不同,那么进行这样的更改是有意义的。

      FileProvider.getUriForFile(mContext.get(), BuildConfig.APPLICATION_ID + ".fileprovider", file)
      

      【讨论】:

      • 从 .provider 更改为 .fileprovider 解决了我的问题。谢谢@ulaserdegor
      • 谢谢。完美工作:)
      【解决方案5】:

      这里的问题是,您使用类名 .provider 作为清单中的权限 并在 java 代码中使用 .fileprovider 类名。

       <provider
              android:name="androidx.core.content.FileProvider"
              android:authorities="${applicationId}.provider"
              android:exported="false"
              android:grantUriPermissions="true">
              <meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/provider_paths_app" />
          </provider>
      
      
      
      Couldn't find meta-data for provider with authority my.package.name.fileprovider
      

      只需将 fileprovider 重命名为 provider

      【讨论】:

        【解决方案6】:

        就我而言;

        清单文件:

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
        

        代码:

        import androidx.multidex.BuildConfig // NOT DO THIS!!!
        
        val uri = FileProvider.getUriForFile(this,
                    BuildConfig.APPLICATION_ID+ ".provider", _tempFile)
        

        例外:

        java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority androidx.multidex.provider
        

        不要使用androidx.multidex.BuildConfig,因为这个值不是我们应用的值:

        //
        // Source code recreated from a .class file by IntelliJ IDEA
        // (powered by Fernflower decompiler)
        //
        
        package androidx.multidex;
        
        public final class BuildConfig {
            public static final boolean DEBUG = false;
            public static final String APPLICATION_ID = "androidx.multidex";
            public static final String BUILD_TYPE = "release";
            public static final String FLAVOR = "";
            public static final int VERSION_CODE = -1;
            public static final String VERSION_NAME = "";
        
            public BuildConfig() {
            }
        }
        

        【讨论】:

        • 谢谢,它对我有用,为了更清楚,我导入:import androidx.multidex.BuildConfig 而不是:import com.myapplication.packagename.BuildConfig;
        【解决方案7】:

        {applicationId} == com.companyName.application 附加“.provider”

        这将是 == com.example.test.provider

        在xml中authorities:com.example.test.provider

        活动中 Uri mPath = FileProvider.getUriForFile(this, "com.example.example.provider", imageFile);

        【讨论】:

          【解决方案8】:

          mImageFromCamera = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", mImageFile); android:authorities="${applicationId}.fileprovider"

          xml 和代码中的权限必须相同

          【讨论】:

            【解决方案9】:

            首先你使用

            File(getExternalFilesDir(null),context?.getString(R.string.app_name));


            而不是

            Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name) // this is deprecated in API29


            那就用这个

            Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

            更多帮助:File Provider Description

            【讨论】:

              【解决方案10】:

              你可以替换你的BuildConfig导入类文件名:

              import androidx.multidex.BuildConfig;
              

              与:

              import com.yourAppName.BuildConfig;
              

              【讨论】:

                【解决方案11】:

                这是因为这个权限名称不同

                   <provider
                        android:name="androidx.core.content.FileProvider"
                        android:authorities="${applicationId}.provider"
                        android:exported="false"
                        android:grantUriPermissions="true"
                        >
                        <meta-data
                            android:name="android.support.FILE_PROVIDER_PATHS"
                            android:resource="@xml/provider_paths"
                            />
                    </provider>
                
                 here Look     
                      
                      <pre>android:authorities="${applicationId}.provider" </pre>
                           
                

                这里的授权名称是你 packagename.provider (com.example.app.provider)

                        val uri: Uri? = FileProvider.getUriForFile(
                        this.contexts,
                        BuildConfig.APPLICATION_ID + ".provider",
                        file
                    )
                

                每当我们复制粘贴代码时,权限可能会从一个来源更改为另一个来源。在stackoverflow中,一些开发人员放入清单 ${applicationId}.provider" 并在uri中放入 (.fileprovider) FileProvider.getUriForFile( this.contexts, BuildConfig.APPLICATION_ID + ".fileprovider", 文件 ) 这是现在日志中显示的权限不同的问题

                【讨论】:

                  【解决方案12】:
                  FileProvider.getUriForFile(this, "{yourPAckageName}.fileprovider",
                                                  file);
                  

                  FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
                                      BuildConfig.APPLICATION_ID + ".provider", file);
                  

                  我花了一天时间寻找解决方案。这部分非常重要。这节省了我的时间。

                  【讨论】:

                    【解决方案13】:

                    解决我的问题的是:

                    用这个替换你的路径应用程序文件: 查看文件根目录,我用我的应用程序包名称确定了路径,将 pkg 名称更改为您的。

                    <?xml version="1.0" encoding="utf-8"?>
                    <paths xmlns:android="http://schemas.android.com/apk/res/android">
                        <external-path
                            name="files_root"
                            path="Android/data/com.my.newproject4" />
                        <external-path
                            name="external_files"
                            path="." />
                    </paths>
                    

                    并且在您的提供商中 使用你的包名而不是 ${applicationId}

                    和我一样

                    <provider
                    android:name="androidx.core.content.FileProvider"
                    android:authorities="com.my.newproject4.provider"
                    android:exported="false"
                    android:grantUriPermissions="true">
                    
                    <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths" />
                    </provider>
                    

                    这对我有用 经过长时间的尝试和寻找,唯一的解决方案和方法 这就是问题所在

                    也许它可以帮助某人 我希望你好运

                    【讨论】:

                      【解决方案14】:

                      将权限更改为唯一名称以解决问题,例如

                      android:authorities="${applicationId}.myUniquefileprovider"

                      也可以在java代码中

                      【讨论】:

                        【解决方案15】:

                        我刚刚从 android:authorities="${applicationId}.provider" 中删除了“$”,它现在就像一个魅力。

                        【讨论】:

                          【解决方案16】:

                          它的文件提供者而不是提供者

                          android:authorities="${applicationId}.fileprovider"
                          

                          【讨论】:

                            猜你喜欢
                            • 2020-05-17
                            • 2020-03-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2020-06-29
                            • 2021-03-06
                            • 2020-08-31
                            • 1970-01-01
                            相关资源
                            最近更新 更多