I am trying to open an .apk file within my expo app, but after downloading it, this just doesn't do anything. I tried to use an intent with images, videos and audios, and it works except with apk files. I'm using Development Build so I'm not using Expo Go.
import * as FileSystem from "expo-file-system";
import { startActivityAsync } from 'expo-intent-launcher';
export default function HomeScreen() {
const downloadAndInstallApk = async () => {
const url = ".apk";
try {
const fileUri = FileSystem.cacheDirectory + "com-zhiliaoapp-musically-go-300702-65618975-98c804aab2784f318a912d822925ca33.apk";
const contentUri = await FileSystem.getContentUriAsync(uri);
console.log("Content URI:", contentUri);
if (Platform.OS === "android") {
// Launch the installer with the proper action
await startActivityAsync("android.intent.action.INSTALL_PACKAGE", {
data: contentUri,
flags: 1, // Grant temporary read permission
});
} else {
console.error("This functionality is only available on Android.");
}
} catch (error) {
console.error("Error downloading or installing APK:", error);
}
};
// The same code but using images. This actually works.
const downloadAndInstallApkImage = async () => {
const url = ".jpg";
try {
const fileUri = FileSystem.documentDirectory + "file_example_JPG_2500kB.jpg";
// Download the file
const { uri } = await FileSystem.downloadAsync(url, fileUri);
console.log("Downloaded to:", uri);
// Convert file:// URI to a content:// URI
const contentUri = await FileSystem.getContentUriAsync(uri);
console.log("Content URI:", contentUri);
if (Platform.OS === "android") {
// Launch the installer with the proper action
await startActivityAsync("android.intent.action.VIEW", {
data: contentUri,
flags: 1, // Grant temporary read permission
});
} else {
console.error("This functionality is only available on Android.");
}
} catch (error) {
console.error("Error downloading or installing APK:", error);
}
};
}