【问题标题】:how to read file from local storage?如何从本地存储中读取文件?
【发布时间】:2019-02-04 16:07:53
【问题描述】:

我正在将图像上传到设备的本地存储 (Android),任务正在成功完成,现在 当我尝试从本地存储读取文件时遇到问题,它会弹出错误 说:

invariation Violation 对象是无效的 React 子对象

这是我上传图片的代码:

let dirs = RNFetchBlob.fs.dirs;
RNFetchBlob.fs.exists(dirs.PictureDir + "/myfolder")
    .then((exist) => {
            //console.log(`file ${exist ? '' : 'not'} exists`);
            if (!exist) {
                try {
                    const fs = RNFetchBlob.fs;
                    /
                    RNFetchBlob.fs.mkdir(dirs.PictureDir + "/myfolder")
                        .then(() => {
                                let base64Str = data;
                                fs.writeFile(dirs.PictureDir + "/myfolder/" + imageName, base64Str, 'base64')
                                    .then(() => {
                                        RNFetchBlob.fs.scanFile([{
                                            path: dirs.PictureDir + "/myfolder/",
                                            mime: 'jpeg/jpg'
                                        }]);
                                    }).catch(() => {
                                        alert("error");
                                    })

这是我的图片的路径

常量路径 = dirs.PictureDir+"/myfolder/1718179779c718ba84098b90f6061816fba9f.jpg"

这里是我访问该图像的代码,请您帮忙

<ScrollView keyboardShouldPersistTaps="handled">

        <View style={styles.containerWithMargin} >
        {RNFetchBlob.fs.readFile(path, 'base64') .then( (data) =>
             <Avatar
             medium
             onPress={this.onSelectPostImage.bind(this)}
             source={{uri : data}}
           />
        )}

        </View>

【问题讨论】:

    标签: react-native


    【解决方案1】:

    RNFetchBlob.fs.readFile 返回一个承诺,任何异步都应该在 ComponentDidMount 中,然后你可以使用 setState 设置 imageUrl,然后在 jsx 中你将拥有类似的东西

    <View style={styles.containerWithMargin} >
    {imageUrl && 
       <Avatar
             medium
             onPress={this.onSelectPostImage.bind(this)}
             source={{uri : imageUrl}}
           />
    }
    </View
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多