android - 无法从资产目录安装 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)
}
解决方案
使用当前的 API,您必须在AndroidManifest.xml
:
<!-- required for installing other packages on Android O -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
...并且用户还需要在设置中启用“从未知来源安装”。
推荐阅读
- javascript - V8 如何为 Map 实现原始值键?
- php - Laravel:如何在 groupBy 查询中显示访问器?
- excel - 如何修复运行时错误“1004”引用在 excel VBA 中无效?
- html - 如何适合整个页面的 select2 多选字段?
- java - 如何同时运行运行脚本和关闭应用程序的异步任务
- boolean-algebra - 如何简化长表达式
- android - 用户快照流中出现意外的 non_personalized_ads - Firebase Analytics
- sql - 获取sql日期之间
- c# - Mongo Atlas - 连接限制低?
- linux - gitlab dosent 恢复备份