joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 772 forks source link

RnFetchBlob.fs.readStream not working on Android 10 devices #702

Open anupamhore opened 3 years ago

anupamhore commented 3 years ago

Hi, I wanted to use RnFetchBlob to read the pdf from the device and upload it to the server. The following code works well in iOS and Android 9 and lower but in devices having Android 10 it is not working. This is the following code

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

        for (const res of results) {

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

                    ifstream.open()
                    ifstream.onData((data)=>{
                        data1 += data;
                    })

                    ifstream.onEnd(() => {

                        let base64 = data1 //`data:${res.type};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;
        }
    }

I have added this in the manifest.xml file for Android

......
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
  **android:requestLegacyExternalStorage="true"**
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:allowBackup="false"
  android:theme="@style/AppTheme">

    <meta-data android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>

  <!-- Add this SplashActivity -->
  <activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
    </intent-filter>
  </activity>

Package json file

"dependencies": { "@invertase/react-native-apple-authentication": "^1.1.2", "@react-native-community/async-storage": "^1.11.0", "@react-native-community/checkbox": "^0.4.2", "@react-native-community/clipboard": "^1.5.0", "@react-native-community/netinfo": "^5.9.5", "axios": "^0.19.2", "lottie-ios": "^3.1.8", "lottie-react-native": "^3.5.0", "moment": "^2.27.0", "react": "16.11.0", "react-native": "0.62.2", "react-native-autoheight-webview": "^1.5.1", "react-native-chart-kit": "^5.6.1", "react-native-chartjs": "^1.0.3", "react-native-crypto-js": "^1.0.0", "react-native-display-html": "^1.2.5", "react-native-document-picker": "^3.5.4", "react-native-fbsdk": "^2.0.0", "react-native-google-signin": "^2.1.1", "react-native-htmlview": "^0.16.0", "react-native-image-crop-picker": "^0.32.0", "react-native-inappbrowser-reborn": "^3.4.0", "react-native-pure-chart": "0.0.24", "react-native-router-flux": "^3.45.0", "react-native-splash-screen": "^3.2.0", "react-native-svg": "^12.1.0", "react-native-svg-charts": "^5.4.0", "react-native-svg-transformer": "^0.14.3", "react-native-switch": "^1.5.0", "react-native-table-component": "^1.2.1", "react-native-vector-icons": "^6.6.0", "react-native-webview": "^10.3.2", "react-redux": "^7.2.0", "redux": "^4.0.5", "redux-thunk": "^2.3.0", "rn-fetch-blob": "^0.12.0", "victory-native": "^35.0.1" }, "devDependencies": { "@babel/core": "^7.6.2", "@babel/runtime": "^7.6.2", "@react-native-community/eslint-config": "^0.0.5", "babel-jest": "^24.9.0", "eslint": "^6.5.1", "jest": "^24.9.0", "metro-react-native-babel-preset": "^0.58.0", "react-test-renderer": "16.11.0" }, "jest": { "preset": "react-native" } }

k13w commented 3 years ago

Me too, the app can't download

anupamhore commented 3 years ago

anyone has solved this issue. Kindly let me know

2mt-hillen commented 3 years ago

the problem you have is scoped storage, there is a solution for android 10 here: https://github.com/joltup/rn-fetch-blob/issues/700#issuecomment-732417928 but this won't work in android 11