Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
e05bf0a9
U
uni-api
项目概览
DCloud
/
uni-api
通知
671
Star
23
Fork
12
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
3
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
3
Issue
3
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
e05bf0a9
编写于
12月 08, 2023
作者:
taohebin@dcloud.io
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 兼容asset目录
上级
de23c355
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
77 addition
and
31 deletion
+77
-31
uni_modules/uni-installApk/changelog.md
uni_modules/uni-installApk/changelog.md
+2
-0
uni_modules/uni-installApk/package.json
uni_modules/uni-installApk/package.json
+1
-1
uni_modules/uni-installApk/utssdk/app-android/index.uts
uni_modules/uni-installApk/utssdk/app-android/index.uts
+74
-30
未找到文件。
uni_modules/uni-installApk/changelog.md
浏览文件 @
e05bf0a9
## 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)
...
...
uni_modules/uni-installApk/package.json
浏览文件 @
e05bf0a9
{
{
"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"
...
...
uni_modules/uni-installApk/utssdk/app-android/index.uts
浏览文件 @
e05bf0a9
import { InstallApkOptions, InstallApkSuccess } from "../interface.uts"
import { InstallApkOptions, InstallApkSuccess } from "../interface.uts"
import { InstallApkFailImpl } from "../unierror.uts"
import { InstallApkFailImpl } from "../unierror.uts"
import Intent from 'android.content.Intent';
import Intent from 'android.content.Intent';
import Build from 'android.os.Build';
import Build from 'android.os.Build';
...
@@ -6,34 +6,78 @@ import File from 'java.io.File';
...
@@ -6,34 +6,78 @@ 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/")) {
let error = new InstallApkFailImpl(1300002);
filePath = filePath.replace("/android_asset/", "")
options.fail?.(error)
apkFile = copyAssetFileToPrivateDir(context, filePath)
options.complete?.(error)
} else {
return
apkFile = new File(filePath)
}
}
const intent = new Intent()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
if (apkFile != null && !apkFile.exists() && !apkFile.isFile()) {
intent.setAction(Intent.ACTION_VIEW)
let error = new InstallApkFailImpl(1300002);
options.fail?.(error)
if (Build.VERSION.SDK_INT >= 24) {
options.complete?.(error)
const authority = context.getPackageName() + ".dc.fileprovider"
return
const apkUri = FileProvider.getUriForFile(context, authority, apkFile)
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
const intent = new Intent()
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
} else {
intent.setAction(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
if (Build.VERSION.SDK_INT >= 24) {
const authority = context.getPackageName() + ".dc.fileprovider"
context.startActivity(intent)
const apkUri = FileProvider.getUriForFile(context, authority, apkFile!!)
const success : InstallApkSuccess = {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
errMsg: "success"
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
}
} else {
options.success?.(success)
intent.setDataAndType(Uri.fromFile(apkFile!!), "application/vnd.android.package-archive");
options.complete?.(success)
}
}
\ No newline at end of file
context.startActivity(intent)
const success : InstallApkSuccess = {
errMsg: "success"
}
options.success?.(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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录