Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 重新编译后无法从旧版本的应用程序加载文件_Android_Ios_React Native_Filesystems_React Native Fs - Fatal编程技术网

Android 重新编译后无法从旧版本的应用程序加载文件

Android 重新编译后无法从旧版本的应用程序加载文件,android,ios,react-native,filesystems,react-native-fs,Android,Ios,React Native,Filesystems,React Native Fs,我的React本机应用程序中存在以下问题。该应用程序将一些PDF文件存储为文件,并能够访问它们。然后可能在重新编译之后,应用程序开始在访问这些文件时出现问题。但是,这些文件仍然存在。我下载了应用程序的完整数据容器进行检查 我怀疑这是因为应用程序的数据容器URI中存在一个动态部分,在重新编译之后,该部分始终会随着实际路径发生变化?e、 g.D22506C1-9364-43A4-B3C7-F9FFF0E1CC48,6BDC3F93-6BC3-4BB6-BD3F-9BFA7E4A4627 如果是这样

我的React本机应用程序中存在以下问题。该应用程序将一些PDF文件存储为文件,并能够访问它们。然后可能在重新编译之后,应用程序开始在访问这些文件时出现问题。但是,这些文件仍然存在。我下载了应用程序的完整数据容器进行检查

我怀疑这是因为应用程序的数据容器URI中存在一个动态部分,在重新编译之后,该部分始终会随着实际路径发生变化?e、 g.
D22506C1-9364-43A4-B3C7-F9FFF0E1CC48
6BDC3F93-6BC3-4BB6-BD3F-9BFA7E4A4627

如果是这样,最好的做法是将URI存储在数据库中的React Native中,以便再次检索它们

以下6个PDF文件:

ListViewItem.js:30 Debug: Report URI /var/mobile/Containers/Data/Application/D22506C1-9364-43A4-B3C7-F9FFF0E1CC48/Documents/Reports/dk79lqddh3mlkcstqel9.pdf
ListViewItem.js:30 Debug: Report URI /var/mobile/Containers/Data/Application/D22506C1-9364-43A4-B3C7-F9FFF0E1CC48/Documents/Reports/e1exw1qg4cs6czktrfkfvi.pdf
ListViewItem.js:30 Debug: Report URI /var/mobile/Containers/Data/Application/D22506C1-9364-43A4-B3C7-F9FFF0E1CC48/Documents/Reports/zfy6hp3zf42me5ru32jfa.pdf
ListViewItem.js:30 Debug: Report URI /var/mobile/Containers/Data/Application/D22506C1-9364-43A4-B3C7-F9FFF0E1CC48/Documents/Reports/fum4qf23mwnzcmye39xau.pdf
ListViewItem.js:30 Debug: Report URI /var/mobile/Containers/Data/Application/D22506C1-9364-43A4-B3C7-F9FFF0E1CC48/Documents/Reports/btksznt1lxv7k4ey23bw93.pdf
ListViewItem.js:30 Debug: Report URI /var/mobile/Containers/Data/Application/6BDC3F93-6BC3-4BB6-BD3F-9BFA7E4A4627/Documents/Reports/smpkiggii4v7xmfhpnmdi.pdf
无法在应用程序的不同位置加载的URI:

例1

<Pdf style={styles.image} source={{ uri: 'file://' + this.props.pdf }} />
这用于写入文件:

FileService.writeFiletoStorage(r.taskId, 'pdf', base64Str)

static writeFiletoStorage(fileName, extention, base64Str) {
    return new Promise((resolve, reject) => {
        RNFS.mkdir(RNFS.DocumentDirectoryPath + '/Reports')
        var path = RNFS.DocumentDirectoryPath + '/Reports/' + fileName + '.' + extention;

        return RNFS.writeFile(path, base64Str, 'base64')
            .then((success) => {
                console.log('FILE WRITTEN!', path, success);
                resolve(path);
            })
            .catch((err) => {
                console.log('Error: unable to write file to storage', path, err.message);
                reject(err)
            });
    })

}

写入文件的方法是返回完整路径,这在不同的编译中有所不同。仅返回相对路径效果更好:

static writeFiletoStorage(fileName, extension, base64Str) {
        return new Promise((resolve, reject) => {
            RNFS.mkdir(RNFS.DocumentDirectoryPath + 'Reports')
            let path = '/Reports/' + fileName + '.' + extension;
            let fullPath = RNFS.DocumentDirectoryPath + path;

            return RNFS.writeFile(fullPath, base64Str, 'base64')
                .then((success) => {
                    console.log('FILE WRITTEN!', fullPath, success);
                    resolve(path);
                })
                .catch((err) => {
                    console.log('Error: unable to write file to storage', fullPath, err.message);
                    reject(err)
                });
        })

    }
必须对读取文件的方法进行相同的更改:

static readFileFromStorage(path, encoding) {
    return new Promise((resolve, reject) => {
        let fullPath = RNFS.DocumentDirectoryPath + path;
        RNFS.readFile(fullPath, encoding)
            .then((file) => {
                resolve(file);
            })
            .catch((err) => {
                console.log('Error: unable to read file', fullPath, err.message);
                reject(err)
            });
    })

}

我也遇到了同样的问题,但是给出动态路径的不是
RNFS.DocumentDirectoryPath
static readFileFromStorage(path, encoding) {
    return new Promise((resolve, reject) => {
        let fullPath = RNFS.DocumentDirectoryPath + path;
        RNFS.readFile(fullPath, encoding)
            .then((file) => {
                resolve(file);
            })
            .catch((err) => {
                console.log('Error: unable to read file', fullPath, err.message);
                reject(err)
            });
    })

}