Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileProvider error

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!

like image 829
rafvasq Avatar asked Jul 22 '16 01:07

rafvasq


People also ask

What is FileProvider in Mac?

The File Provider framework allows you to integrate your cloud storage into the file system on macOS. It uses new APFS features to allow on-demand downloads of user files and folders.

What is FileProvider in Android?

FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri . A content URI allows you to grant read and write access using temporary access permissions.


1 Answers

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.

like image 73
zaifrun Avatar answered Oct 07 '22 18:10

zaifrun