14

https://developer.android.com/training/camera/photobasics.html

All I am trying to do is take a picture with the Camera, save it and display it in an ImageView.

I followed the android tutorial above and keep getting an error (a NullPointerException) on the line:

Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile);

I know I need to configure the FileProvider in my app's manifest and the "authorities" has to match. I don't quite understand what I should be putting in the authorities arguments. I copied all of the code from the tutorial including the file res/xml/file_paths.xml. Ask any questions if necessary.

Thanks!

rafvasq
  • 1,512
  • 3
  • 18
  • 48
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – TheAnonymous010 Jul 22 '16 at 01:22
  • 1
    Nope, question is about how to use FileProvider, not necessarily how to fix a NullPointer – rafvasq Jul 22 '16 at 01:25
  • Glad to see you updated the title accordingly. – TheAnonymous010 Jul 22 '16 at 01:26
  • I have exactly the same problem with this tutorial from Google. I have changed the authorities argument to "mypacketname.fileprovider" in the manifest file in the provider and also have the exact matching string in the call to the getUriForFile, but still get NPE saying this: 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 – zaifrun Aug 04 '16 at 11:47

3 Answers3

28

I finally got it to work!

Remember to put the provider tag INSIDE the application tag in the manifest file - that was my mistake (my provider tag was OUTSIDE of the application tag), and the reason you get this error, which basically says it cannot find the definition of the Provider.

Also make sure you have the correct paths in the .xml file. Here is my version:

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

    <external-path
        name="my_images"   
        path="Android/data/org.projects.cameraapp/files/Pictures" />

</paths>

Of course you have to change the path for your own application.

My actual Provider then looks like this:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="org.projects.cameraapp.fileprovider"          
    android:exported="false"
    android:grantUriPermissions="true">

    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />

</provider>

Again, you'll need to change the authorities value in your own application.

You can see all the source at the GitHub repository from my original question.

stkent
  • 19,772
  • 14
  • 85
  • 111
zaifrun
  • 931
  • 9
  • 21
  • I am generating a file inside my app and want to send it to another app to be saved. What do I put in `android:resource` inside `meta-data` ? – Ishan Feb 11 '17 at 09:00
4

If someone is having hard time as me with support.v4.content.FileProvider you can replace it with this.

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.camera.app.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths">

        </meta-data>
    </provider>
JOX
  • 41
  • 1
1

I also forgot to put <provider> within <application>; I mistakenly put them on the same level which I have since fixed.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="zm.mytestapplication">

<application
    ...
    android:theme="@style/AppTheme">

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="zm.mytestapplication.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
</application>
</manifest>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Pictures/zm/" />
</paths>
Zavvio Mok
  • 11
  • 2