android - 在安装下载的 apk 文件时通过 Intent.getData() 错误暴露在应用程序之外
问题描述
我已经从服务器下载了 apk 文件,现在正在安装它,但它给了我一个错误:通过 Intent.getData() 暴露在应用程序之外
我的代码是
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +
"/download/" + "in.sample.myapp.apk")), "application/vnd.android.package-archive");
我已经搜索了错误并找到了一些但无法解决问题。建议是使用 FileProvider。我这样做只是为了检查日志
Log.d("NewPath", "NewPathName is: " +(FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",outputFile)));
可以帮助解决这个问题。
请帮助和建议。
解决方案
你的代码
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory() + "/FolderName/" + "yourFile.apk");
Uri data = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID +".provider",file);
intent.setDataAndType(data,"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
文件路径.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
显现
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
</application>
推荐阅读
- java - 当我尝试通过 Amazon SES 发送邮件时,总是出现错误
- google-app-engine - 谷歌云平台(GCP)未知错误。原始错误消息:Operation failed: Insufficient CPU quota in region
- c# - Tabcontainer 控件在 ASP.NET 中的服务器上不可见
- testing - 获取solidity函数的返回元素
- macos - iMac 上的 Mysql:找不到命令
- arduino - 超声波传感器始终显示相同的距离
- redux - redux js工具包商店没有更新
- ftp - 同一文件夹中的两个图像可以具有相同的文件名但不同的元数据吗?
- unity3d - 如何使 XR 射线交互器面向不同的方向
- snowflake-cloud-data-platform - Snowflake: JSON data with dynamic columns