react-native-mail icon indicating copy to clipboard operation
react-native-mail copied to clipboard

Issue with Android Permissions during the pdf attachment

Open nameisveeru opened this issue 6 years ago • 7 comments

When attaching the pdf which is created from react-native-html-to-pdf and passing the filePath throws permissions denied for attachments on android. Works fine on IOS.

nameisveeru avatar Aug 23 '18 23:08 nameisveeru

same here , any help !

fedoud32 avatar Sep 27 '18 19:09 fedoud32

same here as well .....

Chetanspace avatar Oct 09 '18 04:10 Chetanspace

Help! plz

ibrhmckc avatar Oct 11 '18 11:10 ibrhmckc

the same. Any updates?

Gazfay avatar Nov 29 '18 10:11 Gazfay

same here. anybody know any workarounds?

brmk avatar Feb 26 '19 19:02 brmk

I have a workaround. You need to use react-native-fs. The solution is to copy the file from the private app cache to the external storage which is readable by other apps.

    let path = pdfFilePath;
    if (Platform.OS !== 'ios') {
      try {
        let hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
        if (!hasPermission) {
          const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            {
              title: i18n.t('write_storage_permission'),
              message: i18n.t('write_storage_permission_message'),
              buttonNegative: i18n.t('cancel'),
              buttonPositive: i18n.t('ok'),
            },
          );
          hasPermission = granted !== PermissionsAndroid.RESULTS.GRANTED;
        }
        if (!hasPermission) {
          handleError(i18n.t('error_accessing_storage'));
          return;
        }
      } catch (error) {
        console.warn(error);
      }

      path = `${RNFS.ExternalStorageDirectoryPath}/project_overview_${Number(new Date())}.pdf`;
      
      try {
        await RNFS.copyFile(pdfFilePath, path);
      } catch(error) {
        handleError(get(error, 'message', error));
        return;
      }
    }

    function handleError(error) {
      if (error === 'not_available') error = i18n.t('mail_not_available');
      Alert.alert(
        i18n.t('error'),
        error,
        [
          { text: i18n.t('ok') },
        ],
        { cancelable: true }
      )
    }
    Mailer.mail({
      subject: i18n.t('FinancingPlanning.loan_request_email_subject'),
      recipients: [],
      body: i18n.t('FinancingPlanning.loan_request_email_body', { name: get(user, 'profile.name', '') }),
      isHTML: true,
      attachment: {
        path,  // The absolute path of the file from which to read data.
        type: 'pdf',   // Mime Type: jpg, png, doc, ppt, html, pdf, csv
        name: 'project_overview.pdf',   // Optional: Custom filename for attachment
      }
    }, (error, event) => {
      if (error) {
        handleError(error);
      }
    });

brmk avatar Feb 27 '19 17:02 brmk

thanks a lot and also i came for the same solution...

On Wed, Feb 27, 2019 at 11:13 PM Ihor Barmak [email protected] wrote:

I have a workaround. You need to use react-native-fs. The solution is to copy the file from the private app cache to the external storage which is readable by other apps.

let path = pdfFilePath;
if (Platform.OS !== 'ios') {
  try {
    let hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
    if (!hasPermission) {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          title: i18n.t('write_storage_permission'),
          message: i18n.t('write_storage_permission_message'),
          buttonNegative: i18n.t('cancel'),
          buttonPositive: i18n.t('ok'),
        },
      );
      hasPermission = granted !== PermissionsAndroid.RESULTS.GRANTED;
    }
    if (!hasPermission) {
      handleError(i18n.t('error_accessing_storage'));
      return;
    }
  } catch (error) {
    console.warn(error);
  }

  path = `${RNFS.ExternalStorageDirectoryPath}/project_overview_${Number(new Date())}.pdf`;

  try {
    await RNFS.copyFile(pdfFilePath, path);
  } catch(error) {
    handleError(get(error, 'message', error));
    return;
  }
}

function handleError(error) {
  if (error === 'not_available') error = i18n.t('mail_not_available');
  Alert.alert(
    i18n.t('error'),
    error,
    [
      { text: i18n.t('ok') },
    ],
    { cancelable: true }
  )
}
Mailer.mail({
  subject: i18n.t('FinancingPlanning.loan_request_email_subject'),
  recipients: [],
  body: i18n.t('FinancingPlanning.loan_request_email_body', { name: get(user, 'profile.name', '') }),
  isHTML: true,
  attachment: {
    path,  // The absolute path of the file from which to read data.
    type: 'pdf',   // Mime Type: jpg, png, doc, ppt, html, pdf, csv
    name: 'project_overview.pdf',   // Optional: Custom filename for attachment
  }
}, (error, event) => {
  if (error) {
    handleError(error);
  }
});

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/chirag04/react-native-mail/issues/118#issuecomment-467960125, or mute the thread https://github.com/notifications/unsubscribe-auth/Ao1_3qHwA4YJ5x3eGLUnNJHrsVeJPy-Jks5vRsPagaJpZM4WKgoR .

Chetanspace avatar Feb 28 '19 06:02 Chetanspace