Expo: Unable to create and Store PDF into Android 11 in ReactNative App

Viewed 1473

I am facing an issue while try to save a PDF file into storage on Android 11 and plus version.

Below is the error, I received when try to save PDF. **

Unable to copy file into external storage. **

Here is the code, which I am using to generate PDF from html and save that into storage.

import * as MediaLibrary from 'expo-media-library'
import * as Sharing from 'expo-sharing'
import * as Print from 'expo-print'

const htmlContent = `
    <!DOCTYPE html>
    <html lang="en">
    <body>
       Hello Writing a PDF file
    </body>
    </html>

    const url = await Print.printToFileAsync({ html: htmlContent })
    try {
        if (Platform.OS === 'ios') {
            await Sharing.shareAsync(url.uri)
        } else {
            const permission = await MediaLibrary.requestPermissionsAsync()
            if (permission.granted) {
                console.log('asset URL ===', url.uri)
                  
                const asset = await MediaLibrary.createAssetAsync(url.uri)
                console.log('asset ===', asset)
               if (asset) {
                    Alert.alert('', `${ 'Document saved at ' + asset.uri}`, [{ text: 'OK' }])
                  }
                }
              }
        } catch (error) {
              console.error(error)
        }

This code works fine and PDF saved into Android (Having OS version < 11) phone storage, but throw error when try to save in Android 11.

I am using "expo": "~41.0.1"

Please help me to understand, what I am missing here. So I can save PDF into all version of Android. Thanks

0 Answers
Related