问题描述
我也跟着在的,但它不会工作。
I followed the example code on the android developer reference on FileProviders but it won't work.
我已经安装路径文件/出口/
中的文件提供的定义在我的清单和引用的文件并在该路径存在。
I have setup the path files/exports/
in the file provider definition in my Manifest and the referenced files does exist at that path.
在我的清单:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.company.app.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
而在 XML / file_paths.xml
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="exports" path="exports/"/>
</paths>
然后我试图让内容URI具有以下code:
Then I try to get the content-uri with the following code:
Java.IO.File exportFile =
new Java.IO.File ("/data/data/com.company.app/files/exports/file.pdf");
Android.Net.Uri exportUri =
Android.Support.V4.Content.FileProvider.GetUriForFile (this, "com.company.app.fileprovider", exportFile);
不过,我得到这个错误:
However I get this error:
Exception of type 'Java.Lang.NullPointerException' was thrown.
at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1160
at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00000] in <filename unknown>:0
at com.company.App.ImageAndSubtitle.cloudStorageDidDownloadFile (System.Object sender, com.company.App.CloudStorageEventArgs args) [0x001f7] in /app_path/Screens/ImageAndSubtitle.cs:187
--- End of managed exception stack trace ---
java.lang.NullPointerException
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:243)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:217)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:130)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4838)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
at dalvik.system.NativeStart.main(Native Method)
更新
从Android源$ C $ C,我可以追捕下来就我现在知道,在 android.support.v4.content.FileProvider.parsePathStrategy此命令(FileProvider.java:243)
将失败并返回空
:
From the Android source code I could hunt that down so far as I now know that this command in android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:243)
will fail and return null
:
final ProviderInfo info = context.getPackageManager().resolveContentProvider(authority, PackageManager.GET_META_DATA);
但我不知道为什么...
But I have no idea why...
推荐答案
我终于找到了如何创建的
I finally found good example code on how to create ContentProviders and FileProviders on https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider
在我的code实际的错误是,在清单文件我有的
标记,但一定要进去。应用程序之外提供商
标记
清单必须是这样的:
The actual error in my code was that in the Manifest file I had the provider
tag outside of the application
tag, but it must be inside.The Manifest must look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.company.app">
<uses-sdk android:minSdkVersion="13" />
<permission
android:name="com.company.app.fileprovider.READ"
android:description="@string/perm_read"
android:label="@string/perm_read_label"/>
<uses-permission android:name="com.company.app.fileprovider.READ"/>
<application android:label="MyApp">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.company.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:readPermission="com.company.app.fileprovider.READ">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
这篇关于FileProvider抛出异常的GetUriForFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!