react-native-mail
react-native-mail copied to clipboard
Issue with Android Permissions during the pdf attachment
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.
same here , any help !
same here as well .....
Help! plz
the same. Any updates?
same here. anybody know any workarounds?
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);
}
});
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 .