Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...