提交 e05bf0a9 编写于 作者: taohebin@dcloud.io's avatar taohebin@dcloud.io

feat: 兼容asset目录

上级 de23c355
## 1.0.4(2023-12-08)
兼容asset目录文件的处理
## 1.0.3(2023-10-27) ## 1.0.3(2023-10-27)
遵循UniError规范 遵循UniError规范
## 1.0.2(2023-10-27) ## 1.0.2(2023-10-27)
......
{ {
"id": "uni-installApk", "id": "uni-installApk",
"displayName": "uni-installApk", "displayName": "uni-installApk",
"version": "1.0.3", "version": "1.0.4",
"description": "uni-installApk", "description": "uni-installApk",
"keywords": [ "keywords": [
"uni-installApk" "uni-installApk"
......
...@@ -6,12 +6,20 @@ import File from 'java.io.File'; ...@@ -6,12 +6,20 @@ import File from 'java.io.File';
import FileProvider from 'androidx.core.content.FileProvider'; import FileProvider from 'androidx.core.content.FileProvider';
import Context from 'android.content.Context'; import Context from 'android.content.Context';
import Uri from 'android.net.Uri'; import Uri from 'android.net.Uri';
import FileOutputStream from 'java.io.FileOutputStream';
export function installApk(options : InstallApkOptions) : void { export function installApk(options : InstallApkOptions) : void {
const context = UTSAndroid.getAppContext() as Context const context = UTSAndroid.getAppContext() as Context
const filePath = UTSAndroid.convert2AbsFullPath(options.filePath) var filePath = UTSAndroid.convert2AbsFullPath(options.filePath)
const apkFile = new File(filePath) var apkFile : File | null = null;
if (!apkFile.exists() && !apkFile.isFile()) { if (filePath.startsWith("/android_asset/")) {
filePath = filePath.replace("/android_asset/", "")
apkFile = copyAssetFileToPrivateDir(context, filePath)
} else {
apkFile = new File(filePath)
}
if (apkFile != null && !apkFile.exists() && !apkFile.isFile()) {
let error = new InstallApkFailImpl(1300002); let error = new InstallApkFailImpl(1300002);
options.fail?.(error) options.fail?.(error)
options.complete?.(error) options.complete?.(error)
...@@ -23,11 +31,11 @@ export function installApk(options : InstallApkOptions) : void { ...@@ -23,11 +31,11 @@ export function installApk(options : InstallApkOptions) : void {
if (Build.VERSION.SDK_INT >= 24) { if (Build.VERSION.SDK_INT >= 24) {
const authority = context.getPackageName() + ".dc.fileprovider" const authority = context.getPackageName() + ".dc.fileprovider"
const apkUri = FileProvider.getUriForFile(context, authority, apkFile) const apkUri = FileProvider.getUriForFile(context, authority, apkFile!!)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else { } else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); intent.setDataAndType(Uri.fromFile(apkFile!!), "application/vnd.android.package-archive");
} }
context.startActivity(intent) context.startActivity(intent)
...@@ -37,3 +45,39 @@ export function installApk(options : InstallApkOptions) : void { ...@@ -37,3 +45,39 @@ export function installApk(options : InstallApkOptions) : void {
options.success?.(success) options.success?.(success)
options.complete?.(success) options.complete?.(success)
} }
function copyAssetFileToPrivateDir(context : Context, fileName : string) : File | null {
try {
const destPath = context.getCacheDir().getPath() + "/apks/" + fileName
const outFile = new File(destPath)
const parentFile = outFile.getParentFile()
if (parentFile != null) {
if (!parentFile.exists()) {
parentFile.mkdirs()
}
}
if (!outFile.exists()) {
outFile.createNewFile()
}
const inputStream = context.getAssets().open(fileName)
const outputStream = new FileOutputStream(outFile)
let buffer = new ByteArray(1024);
do {
let len = inputStream.read(buffer);
if (len == -1) {
break;
}
outputStream.write(buffer, 0, len)
} while (true)
inputStream.close()
outputStream.close()
return outFile
} catch (e : Exception) {
e.printStackTrace()
}
return null
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册