DocumentPicker in React-Native not working properly for Android devices

Viewed 2669

I have implemented a document picker in my react native application and it is working fine for iOS. However in Android, I am having a weird issue

When I open the document picker and the navigation takes to the file explorer (Downloads section) in Android phone, though I am able to select the pdf file but, when it comes to the application back, it stuck to the page and the file is not there. I have attached the screenshot Same behaviour when the file explorer takes to the recent files. Only when from the recent files, I select the google drive and try to select the pdf from there, it works as expected and I can see the file in my application and the app do not stuck.

Here is what I have written for the pdf document picker

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 Can anyone throw some light as to what I may be missing. Is some configuration needs to be set on device level or any other thing.

1 Answers

you can use something like this,

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

now you can call file in your function and to use it you can do like this

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