我正在使用下面的代码来实现带有文件提供程序的代码。在此,我使用 AndroidJavaClass 和 AndroidObjectClass 打开了一个 pdf。我还在清单中设置了文件提供程序,但它给了我错误文件提供程序未找到。请建议我如何解决这个问题。提前致谢。
string filePath = "/storage/emulated/0/Android/data/com.example/files/VF_Test__Group4(2020-09-21_14-40-07).pdf";
string url = "/storage/emulated/0/Android/data/com.exapmle/files/VF_Test__Group4(2020-09-21_14-40-07).pdf";Debug.Log("PaTH File"+ filePath);
Debug.Log("FilePath TestPrint: " + filePath);
Debug.Log("PDFMUN 1 FilePath: " + url);
string dataType = "application/pdf";
AndroidJavaObject clazz = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = clazz.GetStatic<AndroidJavaObject>("currentActivity");
Debug.Log("PDFMUN 2 FilePath: " + url);
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
intent.Call<AndroidJavaObject>("addFlags", intent.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"));
intent.Call<AndroidJavaObject>("setAction", intent.GetStatic<string>("ACTION_VIEW"));
Debug.Log("PDFMUN 3 FilePath: " + url);
var apiLevel = new AndroidJavaClass("android.os.Build$VERSION").GetStatic<int>("SDK_INT");
Debug.Log("PDFMUN 4 FilePath: " + url);
AndroidJavaObject uri;
if (apiLevel > 23)
{
Debug.Log("PDFMUN 5 FilePath: " + url);
AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");
AndroidJavaObject file = new AndroidJavaObject("java.io.File", url);
Debug.Log("PDFMUN 6 FilePath: " + url);
AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
string packageName = unityContext.Call<string>("getPackageName");
string authority = "com.MMD.fileprovider";
Debug.Log("PDFMUN 7 FilePath: " + url);
uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, file);
Debug.Log("PDFMUN 8 FilePath: " + url);
}
else
{
var uriClazz = new AndroidJavaClass("android.net.Uri");
var file = new AndroidJavaObject("java.io.File", url);
uri = uriClazz.CallStatic<AndroidJavaObject>("fromFile", file);
}
Debug.Log("PDFMUN 9 FilePath: " + url);
intent.Call<AndroidJavaObject>("setType", dataType);
intent.Call<AndroidJavaObject>("setData", uri);
currentActivity.Call("startActivity", intent);
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.MMD.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--<external-path name="external_files" path="."/>-->
<external-path path="Android/data/com.MMD" name="files_root" />
<external-path path="." name="external_storage_root" />
<external-path name="external_files" path="."/>
</paths>
我收到一个错误。
2020-09-22 12:19:00.961 9062-9062/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 9062
java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.example-jxtWnex56mQRyHAcj5PXFA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example-jxtWnex56mQRyHAcj5PXFA==/lib/arm, /data/app/com.example-jxtWnex56mQRyHAcj5PXFA==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:6581)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:6133)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6043)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.MMD.Unitycontroller-jxtWnex56mQRyHAcj5PXFA==/base.apk"],nativeLibraryDirectories=[/data/app/com.MMD.Unitycontroller-jxtWnex56mQRyHAcj5PXFA==/lib/arm, /data/app/com.MMD.Unitycontroller-jxtWnex56mQRyHAcj5PXFA==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib]]