android - 除了使用 FileProvider 获取用于安装 Android apk 的 uri 之外,还有其他方法吗?

标签 android uri compatibility android-fileprovider

所以我在尝试安装 apk 时遇到了 uri 暴露的问题。我编写了一个自动更新 android 应用程序的库,然后使用以下代码安装新的 apk。

Uri uri = VersionManagerUtil.getFileDestinationUri(apkDir, mParameters.getPackageName());

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, RC_REQUEST_INSTALL);

但是,由于 FileUriExposedException,这不适用于 api 24+,所以我切换到 FileProvider 来获取 Uri

Uri uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);

这很好用...但是,FileProvider 需要以下依赖项 compile 'com.android.support:support-v4:26.0.0-alpha1' 这是有问题的,因为我想使用这个库的所有项目都需要有相同版本的支持库。那么是否有任何非 hacky 方法来解决 FileUriExposedException 的问题?或者是否有不同的方式来安装 apk 来一起避免这个问题?

最佳答案

However, FileProvider requires the following dependency compile 'com.android.support:support-v4:26.0.0-alpha1'

首先,版本由您决定,当前版本(截至我发布此答案时)是 26.1.0

其次,FileProvider 的实际 Artifact 是 support-core-utils。要求 support-v4 将通过传递依赖项引入 support-core-utils

which is problematic because all of the projects I would like to use this library on would need to have the same version of the support library

或者一个兼容的。 FileProvider 已经存在几年了。

So are there any non-hacky ways to resolve the issue of the FileUriExposedException?

您可以使用 FileProvider。或者,您可以编写自己的 ContentProvider 来提供文件。

Or is there a different way to install an apk that avoids this issue all together?

据我所知不是。

关于android - 除了使用 FileProvider 获取用于安装 Android apk 的 uri 之外,还有其他方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46695990/

相关文章:

java - 当我的应用程序在 Google Play 商店更新时,如何保持显示应用程序的通知?

android - MODE_IN_CALL、MODE_IN_COMMUNICATION、MODE_CALL_SCREENING 有什么区别?

java - 警报对话框首选项未保存

android - 发送 Intent 到另一个应用程序而不在android中打开它

java - 如何将uri图像转换为canvas ondraw方法

android - 获取操作 GET_CONTENT 的文件路径

c# - Uri.IsWellFormedUriString 用于相对 Hashbang url 兼容性

python - 只使用标准库编写 Python 2 和 3 兼容代码的最佳方法

internet-explorer - 兼容模式与 IE 版本仿真

php - 开发相同 PHP 代码的命名空间和非命名空间版本的策略