89

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.

Mehul Kanzariya
  • 888
  • 3
  • 27
  • 58

17 Answers17

208

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);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Pradeep Kumar
  • 2,349
  • 1
  • 10
  • 18
  • 5
    Thank you for this.. I tried all other solutions but `BuildConfig.APPLICATION_ID + ".provider"` is what made it finally work for me. – Jayce Sep 16 '20 at 05:38
  • 2
    For me, the issue was case sensitivity. I hade `.FileProvider` in one place and `.fileprovider` in the other. The case must match! – gMale Oct 07 '20 at 21:57
  • I tried to BuildConfig.APPLICATION_ID + ".provider" is what made it finally work for me – Mohsinali Feb 12 '21 at 14:54
  • 29
    why is it complicated like hell – Aadam Nov 23 '21 at 14:45
  • @Aadam yes I am wondering them deprecate us to use 2 rows but now we have 40 extra lines and it still need something more. – Egor Jun 18 '22 at 14:06
  • Why am I getting this error ?? 'BuildConfig' is inaccessible due to it's protection level – TechOverflow May 07 '23 at 19:22
31
    <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.

Saint
  • 1,492
  • 1
  • 11
  • 20
14

In my case.

I have a library project .Let's call it :LibApk

When I code this : applicationId "my.package.name" in the build.grale(app)

when build gradle tell me that : Library projects cannot set applicationId.

applicationId is set to 'com.darcy.apkupdate' in default config.

So ,I delete applicationId "my.package.name" .

Then ,build.gradle look like this : enter image description here

But I forgot update the AndroidManifest.xml file which use ${applicationId}

enter image description here

This is the problem!!!

So I changed the variable to constant .The result looks like below picture: enter image description here

After This ,It's wokr for me!

Hope this is help you...

linjiejun
  • 1,468
  • 15
  • 22
9

If the build's suffix is different, it makes sense to make a change like this.

FileProvider.getUriForFile(mContext.get(), BuildConfig.APPLICATION_ID + ".fileprovider", file)
ulaserdegor
  • 371
  • 5
  • 10
8

The problem here is, you use the class name .provider for authorities in manifest and use .fileprovider class name in java code.

 <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

Just rename fileprovider to provider

7

In My Case;

Manifest File:

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

Code:

import androidx.multidex.BuildConfig // NOT DO THIS!!!

val uri = FileProvider.getUriForFile(this,
            BuildConfig.APPLICATION_ID+ ".provider", _tempFile)

Exception:

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

Do not use androidx.multidex.BuildConfig, Because this values are not the values of our application:

//
// 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() {
    }
}
  • 1
    Thanks, It works for me, for more clarification, I Import : import androidx.multidex.BuildConfig instead of: import com.myapplication.packagename.BuildConfig; – AmegoDev. Jul 07 '21 at 19:33
3
FileProvider.getUriForFile(this, "{yourPAckageName}.fileprovider",
                                file);

to

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

I spent a day finding a solution. This part is very important. That saved my day.

Can Tosun
  • 31
  • 1
2

{applicationId} == com.companyName.application append ".provider"

which will be == com.example.test.provider

in xml authorities:com.example.test.provider

in activity Uri mPath = FileProvider.getUriForFile(this, "com.example.example.provider", imageFile);

1

This is Because of this authority name difference

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

Here the authority name is you packagename.provider (com.example.app.provider)

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

whenever we copy paste the code the authority may be change from one source to another. in stackoverflow some devs put in manifest ${applicationId}.provider" and in uri they put (.fileprovider) FileProvider.getUriForFile( this.contexts, BuildConfig.APPLICATION_ID + ".fileprovider", file ) this is the problem now authority is different that is showing in log

lava
  • 6,020
  • 2
  • 31
  • 28
1

what solved my problem is :

replace your paths app file by this : take a look at files root, i determined the path with my app package name, change the pkg name to yours.

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

and also in your provider use your package name instead of ${applicationId}

same as me here

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

this is what worked with me the only solution and way after long time of trying and searching just this was the problem

maybe it helps someone i hope for you a good luck

Hasni
  • 187
  • 1
  • 10
0

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

authorities must be same in xml and in code

s0l
  • 61
  • 1
  • 2
  • 4
0

First you use

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


instead of

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


then use this

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

For more help: File Provider Description

Mohit Singh
  • 1,402
  • 1
  • 10
  • 19
0

You can replace your BuildConfig import class file name:

import androidx.multidex.BuildConfig;

with:

import com.yourAppName.BuildConfig;
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
0

I had recently converted my app module into a library module where it applicationId had to be removed from my build.gradle file (of the module which I converted from an app module to a library module).

Below is the provider tag of AndroidManifest file in converted library module

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

Now, the new app module where I am using the library has different applicationId in it's AndroidManifest. So, we need to pass the new applicationId wherever we use FileProvider authority is needed in code.

Or android:authorities in library AndroidManifest needs to be changed to make it exactly same as to FileProvider authority used in our code.

Example Usage:

Uri photoURI = FileProvider.getUriForFile(this, Constants.ExternalFileProviderAuthority, f);
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
-1

Its fileprovider and not provider

android:authorities="${applicationId}.fileprovider"
Gkapoor
  • 840
  • 1
  • 13
  • 27
-2

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

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

also in java code

Vaishakh
  • 1,126
  • 10
  • 10
-2

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

Manish Joshi
  • 85
  • 2
  • 12