Android O中使用FileProvider的一个小变化

在Android O中遇到一个比较有意思的bug,大概描述为:应用A中需要打开一张图片,这时候选择机器中图库或者是谷歌自带的相册都会出现黑屏现象。

10-23 17:16:58.781   812  2012 W ActivityManager: For security reasons, the system cannot issue a Uri permission grant to content://com.xxx.fileProvider/external_storage/Bugreport/endusertest/data/000000000000000_1508737212738/.nomedia/OIMG-1508737223937-5346144739450824145.jpg [user 0]; use startActivityAsCaller() instead

在源码中找到log的出处:

在 ActivityManagerService.java  (AMS) 中的checkGrantUriPermissionLocked()方 法内的如下代码块中打印的:

 

// Bail early if system is trying to hand out permissions directly; it

       // must always grant permissions on behalf of someone explicit.

       finalint callingAppId=UserHandle.getAppId(callingUid);

       if((callingAppId== SYSTEM_UID)||(callingAppId== ROOT_UID)){

           if("com.android.settings.files".equals(grantUri.uri.getAuthority())){

               // Exempted authority for cropping user photos in Settings app

           }else{

               Slog.w(TAG,"For security reasons, the system cannot issue a Uri permission"

                       +" grant to "+ grantUri+"; use startActivityAsCaller() instead");

               return-1;

           }

       }

 

从代码来看,如果APP是root用户,或者具有系统级权限(例如:设置了 android:sharedUserId="android.uid.system" 属 性),并且提供的Uri的authority不是“com.android.settings.files” 时,就会打印上述warning的log.

而应用A设置了 android:sharedUserId="android.uid.system" 属性,这样就相当于应用A的UID就是SYSTEM_UID,又它的的authority不是“com.android.settings.files”,故会返回-1,显示成黑屏。

 

if 的这个判断只是针对com.android.settings.files,且看其中的注释 // Exempted authority for cropping user photos in Settings app: 为了Settings app中裁剪用户photos来豁免授权  。

如果要增加其它的属于SYSTEM_UID或者ROOT_UID的provider是要我们自己可以直接在这个地方再加上

 

综上,最后的解决办法是在AMS的checkGrantUriPermissionLocked()函数中增加应用A中申明的provider:

// Bail early if system is trying to hand out permissions directly; it
        // must always grant permissions on behalf of someone explicit.
        final int callingAppId = UserHandle.getAppId(callingUid);
        if ((callingAppId == SYSTEM_UID) || (callingAppId == ROOT_UID)) {
            if ("com.android.settings.files".equals(grantUri.uri.getAuthority())) {
                // Exempted authority for cropping user photos in Settings app
            //Add-BEGIN by mengqin.zhang
            } else if ("com.xxx.fileProvider".equals(grantUri.uri.getAuthority())) {
                // Exempted authority for user to open photos in A app
                //Add-END
            } else {
                Slog.w(TAG, "For security reasons, the system cannot issue a Uri permission"
                        + " grant to " + grantUri + "; use startActivityAsCaller() instead");
                return -1;
            }
        }

在Android N中是不存在这个问题的,虽然只是从N到O,但是还是有很多细节地方做了修改,故还是要认真分析代码。

 

 

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
好的,下面是一个Android使用FileProvider实现多文件jpg分享的示例代码: 1. 在AndroidManifest.xml文件添加FileProvider的定义: ``` <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` 2. 在res/xml目录下创建file_paths.xml文件,指定要共享的文件的路径: ``` <paths> <external-path name="external_files" path="."/> </paths> ``` 3. 在代码获取要分享的文件的Uri,并设置Intent的Action和Type: ``` // 获取要分享的文件列表,此处假设为fileList ArrayList<Uri> uriList = new ArrayList<>(); for (File file : fileList) { Uri uri = FileProvider.getUriForFile(context, "com.example.fileprovider", file); uriList.add(uri); } // 创建分享的Intent Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); shareIntent.setType("image/jpeg"); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); ``` 在上述代码,如果要分享其他类型的文件,可以将Type设置为对应的MIME类型。同时需要注意,在Intent添加FLAG_GRANT_READ_URI_PERMISSION标志,以便接收方应用可以读取共享的文件。 4. 启动分享的Activity: ``` Intent chooserIntent = Intent.createChooser(shareIntent, "分享图片"); startActivity(chooserIntent); ``` 以上就是一个简单的使用FileProvider实现多文件jpg分享的实现示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值