将apk下载到android文件系统并安装

时间:2018-09-16 12:24:14

标签: android reactjs react-native expo

我正在尝试将android .apk文件下载到内部android文件系统中的downloads文件夹中。我已经尝试了许多事情,例如react-native-fsreact-native-install-apk,expo的FileSystem,目前我正在尝试使react-native-simple-download-manager工作。这是供您参考的链接https://www.npmjs.com/package/react-native-simple-download-manager

Expo的文件系统没有引发任何错误,但由于无法在文件系统上找到.apk,因此我无法访问APK进行安装。

我不太清楚为什么,但是所有这些软件包都使用require语法而不是import。我尝试使用require并像import ReactNativeDownloadManager from 'react-native-simple-download-manager';一样正常导入包,但我不断收到这样的错误,它找不到未定义的函数下载。显然,由于我已经在以下链接https://github.com/master-atul/react-native-simple-download-manager/blob/master/index.jsReactNativeDownloadManager内部进行了查找,因此无法正确导入。如果将鼠标悬停在导入本身上,我还将获得以下提示。

找不到模块'react-native-simple-download-manager'的声明文件。 d:/React/SpringApp/node_modules/react-native-simple-download-manager/index.js隐式具有“ any”类型。

    Could not find a declaration file for module 'react-native-simple-download-manager'. 'd:/React/SpringApp/node_modules/react-native-simple-download-manager/index.js' implicitly has an 'any' type.
  Try `npm install @types/react-native-simple-download-manager` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-native-simple-download-manager';`

我确实尝试在命令行中运行它,但出现此错误。

npm ERR! code E404
npm ERR! 404 Not Found: @types/react-native-simple-download-manager@latest

如果有人可以在正确的方向上指导我解决此问题,或为我提供下载apk的更好方法,请执行!谢谢。

import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    Image,
    View,
    TouchableOpacity,
    AsyncStorage,
    Linking,
    Platform,
} from 'react-native';
import Expo from 'expo';
import StarRating from 'react-native-star-rating';
import {
    Icon
} from 'react-native-elements';

import ReactNativeDownloadManager from 'react-native-simple-download-manager';   

export default class ViewApp extends Component {

    constructor(props) {
        super(props);
        this.state = {
            starCount: this.props.navigation.state.params.app.rating,
            appInfo: [{
                APP_DETAIL_DESC: ""
            }],
            sid: '',
            header: this.props.navigation.state.params.app.title,
        };
    }

    _installAndroid(url) {

        headers = {
             'Authorization': 'Bearer'
         };
         config = {
             downloadTitle: this.props.navigation.state.params.app.title,
             downloadDescription: 'spring app download',
             saveAsName: 'springAppID'+ this.props.navigation.state.params.app.id,
             allowedInRoaming: true,
             allowedInMetered: true,
             showInDownloads: true,
             external: false, //when false basically means use the default Download path (version ^1.3)
             path: "Download/" //if "external" is true then use this path (version ^1.3)
         };

        ReactNativeDownloadManager.download(url = url, headers = headers, config = config).then((response) => {
             console.log('Download success!');
             alert("success");
         }).catch(err => {
   console.log('Download failed!');
   console.log(err)
   alert("fails");
         })
    }

    installApp = () => fetch(`OMITTED`, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                appid: this.props.navigation.state.params.app.id,
                staffno: this.state.sid,
            })
        })
        .then((response) => response.json())
        .then((responseJson) => {
            if (Platform.OS === 'ios') {
                Linking.openURL(JSON.parse(responseJson.GetDownladURLResult));
            }
            else {
                this._installAndroid(JSON.parse(responseJson.GetDownladURLResult));
            }
        })
        .catch((error) => {
            console.error(error);
        });

PS:是的,我知道它说不正确,而不是下载。这是我无法控制的已有服务。

0 个答案:

没有答案