Native Android screenshot/image sharing in Unity using FileProvider

Suneet Agrawal
4 min readMay 28, 2019

--

While implementing native sharing using my previous medium post, “Native Android screenshot/image sharing in Unity” we saw an exception “android.os.FileUriExposedException” in Android 8 (Oreo) and above.

Since Google changed the sharing way using FileProvider to make it more secure, we can not share the file object directly to any other Application. We need to use the FileProvider and grant read URI permission to make it accessible for any other app.

This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-image-sharing-in-unity-using-fileprovider/ and reposted on Medium on 28th May 2020.

In this Medium blog, will guide step by step to enable screenshot sharing to any other app using FileProvider. After this, you can share your screenshots or image to any other apps on above Android 8 (Oreo) also. Please stay with me as it's going to be a long journey.

But before that, I would suggest to please go through my last blogs that will give an insight into what we are trying to achieve.

If you have not read the previous medium posts, I would strongly recommend to read them first. You can read them on the links below.

To use file provider in Unity and share a screenshot/image, we need to follow below steps.
1. Create a project and separate module in Native Android using Android Studio.
2. Define paths in xml file in native android.
3. Define Provider in Android manifest file.
4. Export it as an Android archive file (.aar) from Android Studio.
5. Enable Gradle dependency in Unity project.
6. Add Support dependency in the gradle file.
7. Change sharing intent code in the C# file.

We can skip the first 4 steps and can use “.aar” file generated by me (which I will share) also but to give everyone an insight about what is happening exactly,

Let’s go step by step.

1. Create a project and separate module in Native Android using Android Studio

Since putting a raw Android XML is not allowed in Unity, we need a separate Android module containing our code and export it as an aar file to use it in Unity. To create the same we need Android studio. I am not going to follow on how to download and setup android studio. You can Google it and set it up.

Create a new project in Android studio.

Select the minimum API level same as your unity project and fill all remaining fields. These fields really don’t matter as we are going to create a separate library in this project. Click on finish.

Create new “Android Library Module” in the same project by clicking File > New > New Module.

Name the library and click finish.

Once this is done, we can remove the unnecessary dependencies and folders like “test” and “androidTest” folder. We can also remove the string.xml file. We can even remove the testing dependency from build.gradle of our library.

//testInstrumentationRunner 
// "android.support.test.runner.AndroidJUnitRunner"
and//testImplementation 'junit:junit:4.12'
//androidTestImplementation 'com.android.support.test:runner:1.0.2'
//androidTestImplementation
// 'com.android.support.test.espresso:espresso-core:3.0.2'

Please note that we need appcompat support library. The final build.gradle will be

apply plugin: 'com.android.library'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
}

Please continue reading at

I have uploaded all the code (Android and Unity both) into a public Github repository https://github.com/agrawalsuneet/UnitySharingCenter.

I have also kept my “aar” public which can be directly used in any Unity project and can skip the above 4 steps. Please find the aar at https://github.com/agrawalsuneet/UnitySharingCenter/releases/tag/v1

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--