Couldn't find meta-data for provider with authority

I have integrated Snapchat's Creative Kit in my Android app. After processing, I receive an image from the server in the form of Byte Array which I am saving to the disk and then sending the file to the Snapchat's Creative Kit as shown below.

 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)
    }
}

I have also added provider in the manifest file as shown below:

  <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>

And in the provider_paths_app.xml, I have tried all the possible paths by referring this answer and none of them works.

<?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>

With the above path, I am getting the below error.

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

All I have to do is send this image to Snapchat but I am unable to figure out what I am doing wrong. Any help will be appreciated.


Solution 1:

First, write the following tag in manifest under the <application> tag

 <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>

Then create a xml folder in res and create a file named: provider_paths.xml and then copy paste the code:

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

And now here's where most developers make mistakes: in your script, create your File with:

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

Solution 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>

This is my provider declaration, the value of ${applicationId} is "com.limxtop.research", make sure that the name of authorities is the same with that of the codes below.

            // 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);

So, maybe your codes post here is not complete, there you should pass "my.package.name.fileprovider" as parameter some where.