Get names of paths used by FileProvider - android

I have a FileProvider set up in my manifest:
<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" />
</provider>
and a file_paths.xml as follows:
<paths>
<external-path
name="my-external"
path="." />
<cache-path
name="my-cache"
path="." />
</paths>
I want to then determine whether a Uri is one of these paths. So for example, content://my-cache/blah.jpg is therefore in the cache-path.
Of course, I can do this simply by comparing against a hard-coded string, "my-cache", but is there a way to programmatically obtain the contents of file_paths.xml above?

Related

Android app does not start when file provider added to manifest

This is my manifest file
<application>
<provider
android:authorities="${applicationId}.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
</application>
This is my file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path name="root" path="" />
<external-path name="external_storage_root" path="." />
<external-path name="external_storage_download" path="Download" />
</paths>
When I try and execute my app I get the following errors
Error failed processing manifest. Error resource xml/file_paths
(aka com.companyname.ProjectName:xml/file_paths) not found.
This error is likely caused by an issue with the AndroidManifest.xml
file or an Android manifest generation attribute in a source code
file. Project Name
C:..\source\repos\ProjectName\Properties\AndroidManifest.xml
I searched google and tried alot of solutions but non of them actually worked soo here i am now, hopefully someone can help me.
Did you create a folder named xml in your Resources folder ?
like below:

WARNING: [XmlResourcesTransformer] No mapping for: android/support/FILE_PROVIDER_PATHS

On gradle sync, it errors with:
WARNING: [XmlResourcesTransformer] No mapping for: android/support/FILE_PROVIDER_PATHS
<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>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="my_images"
path="." />
</paths>
How to solve this?
Likely this comes from Jetifier - and disabling it (while feasible) might work around the issue, as there won't be any attempts at mapping it. While android:exported="false" probably doesn't make much sense for a FileProvider, which usually is being supposed to be exported (exposed).
There's a blacklist for the Jetifier (in gradle.properties), in case it cannot be disabled:
android.jetifier.blacklist=android.support.FILE_PROVIDER_PATHS
And there's also an android.jetifier.ignorelist ...not sure what the actual difference is. This is only a guess, as I cannot reproduce the issue, but it still seems to be a quite likely guess.

How to add fileprovider xml file in Xamarin (Android)

I am trying to add the xml folder and an xml file named file_path.xml to implement the Fileprovider (android). However, when I add the following (Resources/xml/file_path.xml) content the application does not start anymore.
<paths>
<files-path name="files" path="."/>
</paths>
I get a null pointer exception as it cant read these resource. It doesnt matter whether I add the provider tag in manifest or not. I am very new in Xamarin.
Go to the properties of this file, select Build Action "AndroidResource".
In android manifest, inside the tag application, do this:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"/>
</provider>
https://developer.android.com/reference/android/support/v4/content/FileProvider

Incorrect FILE_PROVIDER_PATHS

For some reason my update script doesn't work anymore. It basically downloads an .APK file, compares the version with the installed version and when newer -> updates the old app with the new one.
However, since today the FILE_PROVIDER_PATHS doesn't seem to work anymore.
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Hanenberg/latest-version.apk
So my question: what do I need to add to my filepaths.xml to make this work again? I always use the Hanenberg folder inside the root on emulator/real device.
filepaths.xml:
<?xml version="1.0" encoding="utf-8" ?>
<paths>
<files-path path="/" name="external_storage_root" />
<files-path path="/storage/emulated" name="emulated" />
</paths>
AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.myfileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths"/>
</provider>
Try this
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>

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

Categories

Resources