React-Native中的DocumentPicker在安卓设备上无法正常工作

3
我在我的React Native应用程序中实现了一个文档选择器,在iOS上运行良好。但是在Android上,我遇到了一个奇怪的问题。
当我打开文档选择器并导航到Android手机中的文件浏览器(下载部分)时,虽然我能够选择PDF文件,但当回到应用程序时,它卡在页面上,文件不在那里。 我已经附加了截图。当文件浏览器到最近文件时,行为相同。只有当从最近文件中选择Google Drive并尝试从那里选择PDF时,它才按预期工作,并且我可以在我的应用程序中看到文件,应用程序不会卡住。
这是我编写的PDF文档选择器内容。
selectPDF = async ()=>{

      
        var imageList = [...this.state.files];


        try {
            const results = await DocumentPicker.pickMultiple({
            type: [DocumentPicker.types.pdf],
            });
            for (const res of results) {

                    console.log(
                        res.uri,
                        res.type, // mime type
                        res.name,
                        res.size
                    );

                    const fileName = res.uri.replace("file://","");
                    let data1 = ''
                    RnFetchBlob.fs.readStream(
                        fileName,
                        'base64',
                        4095
                    )
                    .then((ifstream)=>{
                        //let data1 = ''
                        ifstream.open()
                        ifstream.onData((data)=>{
                            data1 += data;


                        })

                        ifstream.onEnd(() => {
                           
                            let base64 = data1
                   
                                                     
                            imageList.push({
                                imageName:res.name,
                                image:base64,
                                mime:res.type,
                                size:res.size
                            })

                            this.setState({
                                ...this.state,
                                openCamera:false,
                                lastFileName:imageList[imageList.length - 1].imageName,
                                files:imageList
                            })

                        })
                    })



            }
        } catch (err) {
            if (DocumentPicker.isCancel(err)) {
                    this.closeModal();
            } else {
            throw err;
            }
        }
    }

在此输入图像描述 有人能够解释一下我可能遗漏了什么吗?是否需要在设备级别设置某些配置或其他事项。

1个回答

0

你可以使用类似这样的代码:

const [file, setFile] = useState(null);

const selectFile = async () => {
    try {
      const results = await DocumentPicker.pickMultiple({
        type: [DocumentPicker.types.allFiles],
      });
      setFile(results);
    } catch (err) {
      if (DocumentPicker.isCancel(err)) {
        alert('Canceled');
      } else {
        alert('Unknown Error: ' + JSON.stringify(err));
        throw err;
      }
    }
  };

现在你可以在函数中调用文件,使用方法如下:

<TouchableOpacity
            activeOpacity={0.5}
            onPress={selectFile}>
            <Text>Select File</Text>
</TouchableOpacity>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接