How to add fileprovider xml file in Xamarin (Android) - 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

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:

Upgrade from Xamarin Forms 4.8x to 5.0x throws Android Exception

I upgraded an existing Xamarin Forms 4.8x to XF 5.0x and it is throwing an exception on loading the app in the emulator:
Java.Lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path:
I truncated after emulator: as the rest seemed quite specific to my app.
I am stuck - any help is appreciated!
If you receive the following error, it is because you are using AndroidX. To resolve this error, follow the instructions).
Remove the previous <provider android:name="android.support.v4.content.FileProvider"> tag
(AndroidX) Add the following to your AndroidManifest.xml inside the <application> tags:
<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"></meta-data>
</provider>
Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml. Make sure that this XML file has a Build Action of: AndroidResource.
Add the following code:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>

Directory is not created in Xamarin app (Android)

I need to create a directory to store user video files in Internal Storage.
In some part of the code I have this:
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
System.IO.Directory.CreateDirectory(path);
When I debug, path variable is actually: "/data/data/com.companyname.MyApp/files/Videos"
When I browse device files, the closest target folder is this: "This PC\MyDevice\Internal storage\Android\data"
When I browse that folder, no new folder was created. I was expecting to have com.companyname.MyApp/files/Videos folder structure, but it was not created, even when no exception was thrown.
How can I do it?
Thanks
Jaime
You need to add File provider in the manifest
Define file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="images/"/>
<files-path name="my_docs" path="docs/"/>
</paths>
and add it into manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Finally, I had a misconception about Internal and External storage.
What I was seeing in Windows Explorer is in fact, external storage, so I used:
Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryMovies)
That points to the "Movies" directory where I could save the MP4 file in order to view in the Android device.
Regards
Jaime

Android Studio, Element path must be declared for FileProvider

I rerally didn't want to ask this question, but I couldn't find any solutions.
In my manifest I have declared FileProvider:
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
It requires #xml/provider_paths to work. In my resources folder I have created provider_paths.xml file and copy-pasted this code:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="files" path="."/>
</paths>
However Android Studio IDE throws me that kind of error:
Element paths must be declared
My screenshot:
Move the provider_path.xml from values directory to
res/xml/provider_paths.xml
To specify the directories, start by creating the file filepaths.xml in the res/xml/ subdirectory of your project. In this file, specify the directories by adding an XML element for each directory.
Refer Specify Sharable Directories

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