安装下载的 apk

Install a downloaded apk

Android6.0.1 (API23)

尝试将自动更新添加到我的应用程序并得到奇怪的东西:

  1. 下载文件到

    val fileDownloaded = File(context.filesDir, "update.apk")
    
  2. 然后尝试用 intent 安装 apk

    val intent = Intent(Intent.ACTION_VIEW, fileUri)
    intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
    intent.setDataAndType(fileUri, "application/vnd.android.package-archive")
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or 
            Intent.FLAG_ACTIVITY_NEW_TASK
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            startActivity(intent)
    
  3. 清单的权限

     android.permission.INTERNET
     android.permission.WRITE_EXTERNAL_STORAGE
     android.permission.READ_EXTERNAL_STORAGE
     android.permission.REQUEST_INSTALL_PACKAGES
    
  4. 有错误 "There was a problem parsing the package" 和 logcat 消息:

无法打开“/data/user/0/com.testapp.android.demo/files/update.apk':权限被拒绝 03-11 12:52:18.815 3330-3330/com.android.packageinstaller W/zipro: 打开存档时出错 /data/user/0/com.testapp.android.demo/files/update.apk: I/O 错误 03-11 12:52:18.815 3330-3330/com.android.packageinstaller D/asset: 无法打开 Zip 存档 '/data/user/0/com.testapp.android.demo/files/update.apk ' 03-11 12:52:18.815 3330-3330/com.android.packageinstaller W/PackageInstaller: 解析清单时出现解析错误。停止安装

无法获得我必须拥有的权限...

On android API >=24,通过 FileProvider,一切正常。问题仅出现在 api

较低的设备上

谁能告诉我哪里出了问题以及如何解决。

您正在传递指向旧设备上 internal storage. Third-party apps, such as the package installer, have no rights to this location. Download the APK to external storageFile