Android FileProvider 在 GetUriForFile 上抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21931169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileProvider throws exception on GetUriForFile
提问by codingFriend1
I followed the example code on the android developer reference on FileProvidersbut it won't work.
我遵循了FileProviders上的android 开发人员参考中的示例代码,但它不起作用。
I have setup the path files/exports/
in the file provider definition in my Manifest and the referenced files does exist at that path.
我已经files/exports/
在我的清单中的文件提供程序定义中设置了路径,并且引用的文件确实存在于该路径中。
In my Manifest:
在我的清单中:
<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>
And in xml/file_paths.xml
而在 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>
Then I try to get the content-uri with the following code:
然后我尝试使用以下代码获取 content-uri:
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)
Update
更新
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
:
从 Android 源代码中,我可以找到它,因为我现在知道这个命令android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:243)
将失败并返回null
:
final ProviderInfo info = context.getPackageManager().resolveContentProvider(authority, PackageManager.GET_META_DATA);
But I have no idea why...
但我不知道为什么...
回答by codingFriend1
I finally found good example code on how to create ContentProviders and FileProviders on https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider
我终于在https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider上找到了关于如何创建 ContentProviders 和 FileProviders 的很好的示例代码
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:
我的代码中的实际错误是,在 Manifest 文件中,我的provider
标签位于标签外部application
,但它必须在内部。清单必须如下所示:
<?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>
回答by Gene Bo
In trying to set up a FileProvider for getting a PDF file that is on the device and being able to email it out, I ran into this exception as well. Reading the different answers out there - it seems a bit complicated to get the FileProvider set up correctly by hand (at least this first time around).
在尝试设置 FileProvider 以获取设备上的 PDF 文件并能够通过电子邮件发送时,我也遇到了这个异常。阅读不同的答案 - 手动正确设置 FileProvider 似乎有点复杂(至少这是第一次)。
Finally what worked for me was to copy the components from this example into an empty new project: https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PdfShare
最后对我有用的是将本示例中的组件复制到一个空的新项目中:https: //github.com/IanDarwin/Android-Cookbook-Examples/tree/master/PdfShare
Once I see it working in the standalone, then I'm able to bring in the changes to my actual project.
一旦我看到它独立运行,我就可以将更改引入到我的实际项目中。
Specifically, here are the files to replicate:
具体来说,这里是要复制的文件:
1. AndroidManifest.xml- where all the tricky FileProvider config is
1. AndroidManifest.xml-所有棘手的 FileProvider 配置都在这里
2. file_paths.xml- This is a small but important component. Looking on SO there are a variety of answers for what to put there, however only from this project did the set up work for me
2. file_paths.xml-这是一个小而重要的组件。看着SO,有各种各样的答案可以放在那里,但是只有从这个项目中才能为我完成设置工作
3. MainActivity.java- the corresponding Java setup to work with what's configured in the manifest; and of course don't forget the associated layout file
3. MainActivity.java-相应的 Java 设置,用于处理清单中配置的内容;当然不要忘记关联的布局文件
回答by RasenKoD
This error occurred in my case because I had passed an incorrect package name to the second parameter of GetUriForFile
. I used BuildConfig.PACKAGE_NAME
which resolved to android.support.multidexwhich is incorrect.
在我的情况下发生此错误是因为我将错误的包名称传递给GetUriForFile
. 我使用BuildConfig.PACKAGE_NAME
which 解析为android.support.multidex,这是不正确的。