在AndroidManifest。xml添加providerproviderandroid:nameandroidx。core。content。FileProviderandroid:authorities包名。fileProviderandroid:exportedfalseandroid:grantUriPermissionstruemetadataandroid:nameandroid。support。FILEPROVIDERPATHSandroid:resourcexmlfilepathsprovider filepaths文件:?xmlversion1。0encodingutf8?pathsxmlns:androidhttp:schemas。android。comapkresandroidexternalpathnameexternalfilespath。paths安装APKFilecompleteApkfilenewFile(Environment。getExternalStoragePublicDirectory(Environment。DIRECTORYDOWNLOADS),aApkName);if(!completeApkfile。exists()){}completeApkfile。renameTo(newFile(Environment。getExternalStoragePublicDirectory(Environment。DIRECTORYDOWNLOADS),aApkName。replace(。md,。apk)));FileapkfilenewFile(Environment。getExternalStoragePublicDirectory(Environment。DIRECTORYDOWNLOADS),aApkName。replace(。md,。apk));通过Intent安装APK文件IntentintentnewIntent(Intent。ACTIONVIEW);intent。addFlags(Intent。FLAGACTIVITYNEWTASK);UriapkUAndroid7。0以上不支持file:协议需要通过FileProvider访问sd卡下面的文件,所以Uri需要通过FileProvider构造,协议为content:if(Build。VERSION。SDKINTBuild。VERSIONCODES。N){声明需要的临时权限intent。addFlags(Intent。FLAGGRANTREADURIPERMISSION);content:协议apkUriFileProvider。getUriForFile(mContext。getApplicationContext(),包名。fileProvider,apkfile);}else{file:协议apkUriUri。fromFile(apkfile);}intent。setDataAndType(apkUri,applicationvnd。android。packagearchive);aContext。startActivity(intent);