Search code examples
reactjsreact-nativefilesystemsexpoqr-code

Download Base64 Qr Code in Expo React Native


I am creating expo react native app where i generate Qr Code. After it generate when user click save button i want to save qr code in phone. I try this code but but it give Expected URL scheme 'http' or 'https' but was 'data'

Below is my code

downloadFile(){
    const uri = "data:image/pdf;base64"+base64_qr
    let fileUri = FileSystem.documentDirectory + "QRCode.pdf";
    FileSystem.downloadAsync(uri, fileUri)
    .then(({ uri }) => {
        this.saveFile(uri);
      })
      .catch(error => {
        console.error(error);
      })
}

saveFile = async (fileUri) => {
    const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
    if (status === "granted") {
        const asset = await MediaLibrary.createAssetAsync(fileUri)
        await MediaLibrary.createAlbumAsync("Download", asset, false)
    }
}

Can someone helps me?


Solution

  •    async downloadFile(){
          try {
              let filePath = await Print.printToFileAsync({
                  html:' <div style = "margin-top: 40%; margin-left: 30%;"><h2 style = "margin-left: 50px; font-size: 45px;">LetMeIn</h2>'
                      +'<img src="' + base64_qr +'"'
                      + 'alt="Red dot" style = "margin-left: 20px; margin-top: 10px;" />'
                      + '<h2 style = "margin-top: 50px;">Scan the QR Code to check in</h2>'
                      + '</div>',
                  width : 612,
                  height : 792,
              });
    
              const pdfName = `${filePath.uri.slice(
                  0,
                  filePath.uri.lastIndexOf('/') + 1
              )}QRCode.pdf`
    
              await FileSystem.moveAsync({
                  from: filePath.uri,
                  to: pdfName,
              })
    
              console.log('PDF Generated', pdfName);
              this.saveFile(pdfName);
    
          } catch (error) {
              console.error(error);
          }
    
      }