首页 > 解决方案 > 无法从资产目录安装 apk

问题描述

我想从 assets 目录安装 .apk。首先我将 .apk 文件从 assets 目录复制到内部存储或 sdCard 然后尝试安装它,但由于未知的应用程序安装权限应用程序没有安装

val assetManager: AssetManager = assets
        try {
            var file = File(path)
            file.mkdir()

            var outputFile = File(path, "testing_app.apk")

            if (outputFile.exists()) {
                outputFile.delete()
            }
            var inputStream: InputStream = assetManager.open("testing_app.apk")
            var outputStream: OutputStream

            outputStream = FileOutputStream(outputFile)
            var byteArray = ByteArray(1024)

            while (true) {
                val read: Int = inputStream.read(byteArray)
                if (read < 0) {
                    break
                }
                outputStream.write(byteArray, 0, read)

            }

            inputStream.close()

            outputStream.flush()
            outputStream.close()

            var intent = Intent(Intent.ACTION_VIEW)

            intent.setDataAndType(
                Uri.fromFile(File("${path}/testing_app.apk")),
                "application/vnd.android.package-archive"
            )

            startActivity(intent)

        } catch (e: Exception) {
            Log.d("AppError", e.message)

        }

标签: android

解决方案


使用当前的 API,您必须在AndroidManifest.xml:

<!-- required for installing other packages on Android O -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

...并且用户还需要在设置中启用“从未知来源安装”。


推荐阅读