Couldn't find meta-data for provider with auth

2020-03-10 05:53发布

问题:

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.

回答1:

    <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 ${application} 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.



回答2:

First write the following tag in manifest under the 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 names: provide+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 most developer mistake in programs to create File in program then we will use:

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

Hope this will work for yor !!!



回答3:

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

authorities must be same in xml and in code



回答4:

I just removed the '$' from android:authorities="${applicationId}.provider" and it works like a charm now.



回答5:

Change the authorities to a unique name to solve the issue like

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

also in java code