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

标签 android ios react-native filesystems react-native-fs

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

data container of the app

我怀疑这是因为应用程序的数据容器 URI 的动态部分在重新编译后总是会发生变化,后面跟着实际路径?例如D22506C1-9364-43A4-B3C7-F9FFF0E1CC486BDC3F93-6BC3-4BB6-BD3F-9BFA7E4A4627

如果是这样,将 React Native 中的 URI 存储在数据库中以便再次检索它们的最佳实践是什么?

以下 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 }} />

示例2

FileService.readFileFromStorage('file://' + this.report.report_uri, 'base64')    

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

}

这用于写入文件:

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)
            });
    })

}

关于android - 重新编译后无法从旧版本的应用程序加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57792801/

相关文章:

ios - 检测到僵尸 - 但从未调用函数

android - 弹出路径占据所有屏幕

ios - React Native 显示黑屏

ios - 使用 React Native iOS Release Build 不会加载新代码,但版本会加载

javascript - 是否可以在Node.js上使用js资源或者react Native?

android - MyFirebaseMessagingService 不工作但显示通知

iphone - 如何在不卸载 super View 的情况下从 super View 中删除 View ?

java - 在 Java 中同时对三个 ArrayLists 进行排序的最有效方法是什么

android - 生产应用上的 Google Analytics 问题

java - Android,setVisibility/动画问题