最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I open an apk file within Expo App (SDK 52)? - Stack Overflow

programmeradmin4浏览0评论

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);
    }
  };
}
发布评论

评论列表(0)

  1. 暂无评论