首页 > 解决方案 > 从 React Native 中的内容提供者获取数据

问题描述

可以建议任何解决方案或代码示例以从 React Native 中的内容提供者 android 获取数据。目前,我使用“react-native-fs”,但它无法从内容提供者那里获取数据,或者我可能对这个库有误。我的代码

const uriContent = 'content://xxx.provider/info_all'
const existFile = await RNFS.exists(uriContent)
    if (existFile) {
        const fileContent = await RNFS.readFile(uriContent, 'utf8')
        try {
            const newData = JSON.parse(fileContent)
            // console.log('GOT RESULT UUID', newData.uuid);
            if (newData !== undefined && newData !== null &&
                newData.uuid !== undefined && newData.uuid !== null && newData.uuid.length > 0) {
                return newData.uuid
            } else {
                return ''
            }
        } catch (error) {
            console.log(error.message, error.code)
            return ''
        }
    } else {
        return ''
    }

标签: react-native

解决方案


您可以使用 RNFS.stat(uriContent) 代替 RNFS.exists(uriContent)。

State 方法将像这样https://www.npmjs.com/package/react-native-fs#statfilepath-string-promisestatresult提供文件状态


推荐阅读