0

FileProvider.getUriForFile is returning a nullobject reference and I have no idea why. This worked before. The following error is what I get. I also added my code below the error.

Process: com.example.joche.mywarranty, PID: 2522
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
                                                                            at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
                                                                            at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
                                                                            at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
                                                                            at com.example.joche.mywarranty.InputScreen$2.onClick(InputScreen.java:74)
                                                                            at android.view.View.performClick(View.java:5610)
                                                                            at android.view.View$PerformClick.run(View.java:22265)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Code:

imageUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.inputName.provider", new File(fileinputName));

AndroidManifest.xml file:

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

And ofcours the xml file with the path:

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="."/></paths>
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Jochem
  • 65
  • 2
  • 9
  • I disagree with the duplicate question on **What is NullPointerException** since the OP knew the object was null when he said "FileProvider.getUriForFile is returning a nullobject reference and I have no idea why." The question was more like "Why the method is returning null" – Eduardo Herzer Dec 27 '17 at 11:13

1 Answers1

9

In the manifest you declare it as android:authorities="${applicationId}.my.package.name.provider"

and you are trying to use it as a different name: context.getApplicationContext().getPackageName() + ".my.package.inputName.provider"

Both must match. Pick one and use on both places: .my.package.name.provider or .my.package.inputName.provider

Eduardo Herzer
  • 1,683
  • 14
  • 22