Question by
SparrowRed · Apr 20, 2020 at 12:03 PM ·
androidfileandroid-manifestintent
Problem with file provider class
Hello, Can anyone help me with this problem?
I am trying to open a single pdf file using the FileProvider class. I have seen many similar questions, but have not been able to resolve the issue.
If I try to run an application without a provider tag in AndroidManifest.xml, I got this error: java.lang.ClassNotFoundException: android.support.v4.content.FileProvider. Otherwise, the application crashes on start.
Here is my Intent message:
using (var intentClass = new AndroidJavaClass("android.content.Intent"))
{
using (var intentObject = new AndroidJavaObject("android.content.Intent", intentClass.GetStatic<string>("ACTION_VIEW")))
{
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
string packageName = unityContext.Call<string>("getPackageName");
string authority = packageName + ".fileprovider";
int FLAG_ACTIVITY_NEW_TASK = intentObject.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK");
int FLAG_GRANT_READ_URI_PERMISSION = intentObject.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");
AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", uri);
AndroidJavaClass fileProvider = new AndroidJavaClass("android.support.v4.content.FileProvider");
AndroidJavaObject javaUri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);
intentObject.Call<AndroidJavaObject>("setDataAndType", javaUri, "application/vnd.android.package-archive");
intentObject.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK);
intentObject.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);
try
{
currentActivity.Call("startActivity", intentObject);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
}
}
AndroidManifest.xml, provider tag:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.unity3d.player.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths.xml:
<files-path name="my_docs" path="docs/"/>
<external-files-path name="my_documents" path="Documents" />
<external-path name="external_files" path="."/>
Comment
Your answer
Follow this Question
Related Questions
Error to import Unity project with Vuforia to Android Studio 3.0 0 Answers
I am getting an error when upgrading the Unity version. 0 Answers
Android recents tab color 1 Answer
Play Store AdMob permissions 0 Answers
Daydream App Crashes on starting 1 Answer