0

I am trying to get the URI for a file like this:

Uri photoURI = FileProvider.getUriForFile(this,
                        "br.com.[blablabla].sgaconsultor",
                        createImageFile());

private File createImageFile() throws IOException {
        // Create an image file name
        String imageFileName = "registroFoto";

        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);

        return File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                directory
        );
    }

I have a file provider:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="br.com.[blablabla].blabla"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

And the xml is:

<paths>
    <files-path name="br.com.[blablabla].blabla" path="app_imageDir" />
</paths>

But when I run this code I always get this error:

java.lang.IllegalArgumentException: Android: FileProvider IllegalArgumentException Failed to find configured root that contains /data/data/**/files/Videos/final.mp4

But no luck so far... So how can I fix this and get the URI for my file?

Any help is really appreciated!

Community
  • 1
  • 1
Leandro Borges Ferreira
  • 12,422
  • 10
  • 53
  • 73

1 Answers1

3

So how can I fix this and get the URI for my file?

Step #1: Get rid of the useless stuff in the Java code.

Step #2: Get the Java code and the file_paths resource to match, as file_paths says that the file path has to be under getFilesDir() and have app_imageDir in it, and your Java code does not have either of those things.

So, try this:

private File createImageFile() throws IOException {
    // Create an image file name
    String imageFileName = "registroFoto";
    File directory = new File(getFilesDir(), "app_imageDir");

    directory.mkdirs();

    return File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            directory
    );
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491