当前位置:   article > 正文

Android 报错:For security reasons, the system cannot issue a Uri permission

for security reasons, the system cannot issue a uri permission

Android 报错:For security reasons, the system cannot issue a Uri permission

在Android使用activity中进行apk进行安装的时候,出现的报错。
原因是android.uid.system限制了使用该方法。
修改的方法:
frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java

   final int callingAppId = UserHandle.getAppId(callingUid);
        if ((callingAppId == SYSTEM_UID) || (callingAppId == ROOT_UID)) {
            if ("com.android.settings.files".equals(grantUri.uri.getAuthority()) || "com.xxxxxxxx.fileProvider".equals(grantUri.uri.getAuthority())) {
                // Exempted authority for
                // 1. cropping user photos and sharing a generated license html
                //    file in Settings app
                // 2. sharing a generated license html file in TvSettings app
            } else {
                Slog.w(TAG, "For security reasons, the system cannot issue a Uri permission"
                        + " grant to " + grantUri + "; use startActivityAsCaller() instead");
                return -1;
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

修改:其中com.xxxxxxxx.fileProvider就是你要加入的FileProvider的Authority,这是唯一值。
其中在自己的代码安装apk是这样写的:

					File apkFile = new File("/sdcard/downapp/apkfile.apk");
					Uri apk_fileUri = null;
					String install_file_provider_authority = "com.xxxxxxxx.fileProvider";
					try {
						apk_fileUri = FileProvider.getUriForFile(mContext, install_file_provider_authority, apkFile);
					} catch (Exception e) {
						e.printStackTrace();
					}
						Intent intent = new Intent();
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
						intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
						intent.setAction(android.content.Intent.ACTION_VIEW);
						intent.setDataAndType(apk_fileUri, "application/vnd.android.package-archive");
						mContext.startActivity(intent);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

其中
AndroidManifest.xml

  		<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.xxxxxxxx.fileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
         </provider>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

对应的file_paths文件:
对应的地址是:/sdcard/downapp/

<paths>
    <external-path
        name="sky_down_app_file"
        path="./downapp/" />
</paths>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样就完成了错误的修改!完美!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/188970
推荐阅读
相关标签
  

闽ICP备14008679号