android – 意图FLAG_GRANT_READ_URI_PERMISSION在Gingerbread中使用FileProvider

前端之家收集整理的这篇文章主要介绍了android – 意图FLAG_GRANT_READ_URI_PERMISSION在Gingerbread中使用FileProvider前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@
我有一个问题,我试图通过使用在我的清单中声明的​​FileProvider并授予读取uri权限,将多个文件从我的内部存储附加到电子邮件的意图.这是我的代码

表现

<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>

意图创作

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标志我加入的意图是不行的?

我也认为这个问题可能是因为我把这个uri作为EXTRA_STREAM的意图,由于某些原因,较旧的Android版本不会选择这个? (尚未找到任何文件).

任何帮助是赞赏.

解决方法

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,Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
原文链接:https://www.f2er.com/android/309901.html

猜你在找的Android相关文章