意图FLAG_GRANT_READ_URI_PERMISSION在Gingerbread中使用FileProvider

我有一个问题,我试图从我的内部存储附加多个文件到电子邮件意图通过提供他们使用我的清单中声明的​​FileProvider并授予读取的uri权限。 这是我的代码。

performance

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.company.example.logprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> 

意图创build

  emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {"randomemailaddress"}); ArrayList<Uri> uris = new ArrayList<Uri>(); File directory = getFilesDir(); File[] list = directory.listFiles(); for (File f : list) { if (f.getName().contains("log") && f.getName().contains(".txt")) uris.add(FileProvider.getUriForFile(this, "com.company.example.logprovider",f)); } emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 

问题

这可以在运行Jelly Bean的两个设备上正常工作,但对于运行姜饼的两个设备不起作用。

我得到的例外如下。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gm/com.google.android.gm.ComposeActivity}: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{40b2db08 3885:com.google.android.gm/10110} (pid=3885, uid=10110) requires null or null E/AndroidRuntime( 3885): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659) E/AndroidRuntime( 3885): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) E/AndroidRuntime( 3885): at android.app.ActivityThread.access$1500(ActivityThread.java:121) E/AndroidRuntime( 3885): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) E/AndroidRuntime( 3885): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 3885): at android.os.Looper.loop(Looper.java:138) E/AndroidRuntime( 3885): at android.app.ActivityThread.main(ActivityThread.java:3701) E/AndroidRuntime( 3885): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 3885): at java.lang.reflect.Method.invoke(Method.java:507) E/AndroidRuntime( 3885): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) E/AndroidRuntime( 3885): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) E/AndroidRuntime( 3885): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime( 3885): Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{40b2db08 3885:com.google.android.gm/10110} (pid=3885, uid=10110) requires null or null E/AndroidRuntime( 3885): at android.os.Parcel.readException(Parcel.java:1322) E/AndroidRuntime( 3885): at android.os.Parcel.readException(Parcel.java:1276) E/AndroidRuntime( 3885): at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:1882) E/AndroidRuntime( 3885): at android.app.ActivityThread.getProvider(ActivityThread.java:3365) E/AndroidRuntime( 3885): at android.app.ActivityThread.acquireProvider(ActivityThread.java:3390) E/AndroidRuntime( 3885): at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:1728) E/AndroidRuntime( 3885): at android.content.ContentResolver.acquireProvider(ContentResolver.java:754) E/AndroidRuntime( 3885): at android.content.ContentResolver.query(ContentResolver.java:262) E/AndroidRuntime( 3885): at com.google.android.gm.ComposeArea.addAttachment(ComposeArea.java:736) E/AndroidRuntime( 3885): at com.google.android.gm.ComposeArea.initFromExtras(ComposeArea.java:699) E/AndroidRuntime( 3885): at com.google.android.gm.ComposeActivity.initFromExtras(ComposeActivity.java:1482) E/AndroidRuntime( 3885): at com.google.android.gm.ComposeActivity.finishOnCreateAfterAccountSelected(ComposeActivity.java:1020) E/AndroidRuntime( 3885): at com.google.android.gm.ComposeActivity.onCreate(ComposeActivity.java:259) E/AndroidRuntime( 3885): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) E/AndroidRuntime( 3885): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) E/AndroidRuntime( 3885): ... 11 more 

我的想法

所以在这一行的某处,我添加到意图的FLAG_GRANT_READ_URI_PERMISSION标志不起作用?

我也相信这个问题可能是因为我把uris附加到了EXTRA_STREAM的意图之中,而且由于某种原因,旧版的Android版本不会select这个呢? (还没有find任何文件呢)。

任何帮助表示赞赏。

Solutions Collecting From Web of "意图FLAG_GRANT_READ_URI_PERMISSION在Gingerbread中使用FileProvider"

 context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); //revoke permisions context.revokeUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); 

作为最后的手段,如果您不能提供软件包名称,则可以将权限授予所有可处理特定意图的应用程序:

 //grant permisions for all apps that can handle given intent Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); ... List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); }