【发布时间】:2022-11-30 01:22:17
【问题描述】:
我需要从 Expo 应用程序中安装下载的 .apk 文件(用于更新功能)。这是我的代码:
import React from "react";
import { Button, View } from "react-native";
import * as FileSystem from "expo-file-system";
import { startActivityAsync } from "expo-intent-launcher";
export function Updater() {
async function updateApk() {
const uri = "https://expo.dev/artifacts/eas/my_apk_name.apk";
const localUri = FileSystem.documentDirectory + "test.apk";
try {
await FileSystem.downloadAsync(uri, localUri);
const apkUri = await FileSystem.getContentUriAsync(localUri);
await startActivityAsync("android.intent.action.INSTALL_PACKAGE", {
data: apkUri,
flags: 1,
});
} catch (error) {
alert(`Error during installing APK: ${error}`);
}
}
return (
<View>
<Button title="Reset APK" onPress={updateApk} />
</View>
);
}
它下载文件,存储它,但在startActivityAsync期间出现错误:
Encountered an exception while calling native method:
Exception occurred while executing exported method startActivity on module ExpoIntentLauncher:
file://data/user/0/com.my.app.id/files/test.apk exposed beyond app through Intent.getData()
我尝试先将 uri 传递给 FileSystem.getContentUriAsync() 但随后没有错误,意图结果是 0 但没有任何反应。
我在app.json的权限:
"permissions": [
"READ_EXTERNAL_STORAGE",
"WRITE_EXTERNAL_STORAGE",
"CAMERA"
]
我需要任何额外的权限才能让它工作吗?还是世博会完全不可能?也许我应该将文件保存到不同的位置才能使用这个意图?
我也尝试过android.intent.action.VIEW,但没有成功。
我在 Android 13 的物理设备上对其进行了测试。应用程序是用 EAS 构建的。
【问题讨论】:
标签: android react-native expo