react-native - React-Native 中的 DocumentPicker 无法在 Android 设备上正常工作

标签 react-native upload document picker

我在我的 React Native 应用程序中实现了一个文档选择器,它在 iOS 上运行良好。但是在 Android 中,我遇到了一个奇怪的问题

当我打开文档选择器并且导航转到 Android 手机中的文件资源管理器(下载部分)时,虽然我能够选择 pdf 文件,但是当返回应用程序时,它停留在页面上并且该文件不存在。我附上了文件资源管理器对最近的文件采取相同行为的屏幕截图。只有当我从最近的文件中选择谷歌驱动器并尝试从那里选择 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;
            }
        }
    }

enter image description here 谁能对我可能遗漏的东西有所了解。是否需要在设备级别或任何其他方面设置某些配置。

最佳答案

你可以使用这样的东西,

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>

关于react-native - React-Native 中的 DocumentPicker 无法在 Android 设备上正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64889699/

相关文章:

upload - 如何使用 CURL 将文件上传到受密码保护的 HTTPS 站点?

java - Java Swing 的文本 API 是否适合显示自动生成的文本?

r - 如何从 R 生成文档(.rtf、.doc、.odt)

android-actionbar - android webview : prevent text selection actionMode actionBar

ios - 在 iPhone 上运行 React Native 应用程序

javascript - react native 内置二维码生成器不工作

react-native - 如何使用@sentry/react-native 在我的模拟器中打开用户反馈窗口

c# - System.Net.Http.Handlers.ProgressMessageHandler 立即返回 100%

jQuery : File Input Value needed?

javascript - document.write ('<div id="容器"') 在 IE7 中出现问题,它会自动关闭