Monday, July 11, 2016

React native - send photo per mail

Leave a Comment

i am trying to send a recently in app - captured photo by mail and am encountering the following error:

(for the mailing functionality i am using this module : var Mailer = require('NativeModules').RNMail;

I am trying to send a photo by mail with the help of this module and get the following error:

index.ios.bundle:28842Exception '-[MFMailComposeInternalViewController addAttachmentData:mimeType:fileName:] attachment must not be nil.' was thrown while invoking mail on target RNMail with params (         {         attachment =         {             name = Ladunek;             path = "assets-library://asset/asset.JPG?id=3B7DBB2E-1271-4D86-A5F2-A0CEEE7CC4DE&ext=JPG";             type = jpg;         };         body = "body";         isHTML = 1;         recipients =         (             "placeholder@mail.com"         );         subject = Ladunek;     },     9 ) 

This is the invoking code :

.then((data, path) => {         console.log(data)         console.log(data.path)         Mailer.mail({           subject: 'Ladunek',           recipients: ['placeholder@mail.com'],           body: 'body',           isHTML: true, // iOS only, exclude if false           attachment: {             path: data.path,  // The absolute path of the file from which to read data.             type: 'jpg',   // Mime Type: jpg, png, doc, ppt, html, pdf             name: 'Ladunek',   // Optional: Custom filename for attachment           }         }, (error, event) => {             if(error) {               AlertIOS.alert('Error', 'Could not send mail. Please send a mail to support@example.com');             }         });       }) 

Is the path invalid? Or might it be something else.

EDIT

I am obtaining the file path with this module react-native-camera like so:

Event:

takePicture() {     this.camera.capture()       .then((data, path) => 

Element:

<Camera               ref={(cam) => {                 this.camera = cam;               }}               style={{                     flex: 1,                     justifyContent: 'flex-end',                     alignItems: 'center',                     height: 400,                     width: Dimensions.get('window').width                   }}               aspect={Camera.constants.Aspect.fill}>               <Text style={{                         flex: 0,                         backgroundColor: '#fff',                         borderRadius: 5,                         color: '#000',                         padding: 10,                         margin: 40                       }} onPress={this.takePicture.bind(this)}>{cameraIcon}</Text>             </Camera> 

UPDATE2

After including an obj-c file for uri to path transformation I am receiving the following error now:

ExceptionsManager.js:76 JSON value '<null>' of type NSNull cannot be converted to NSString 

Did I "remove" the wrong lines from the following code? :/

Obj-c File content:

#import "RCTBridgeModule.h" #import <AssetsLibrary/AssetsLibrary.h> #import <UIKit/UIKit.h> @interface ReadImageData : NSObject <RCTBridgeModule> @end  @implementation ReadImageData  RCT_EXPORT_MODULE();  RCT_EXPORT_METHOD(readImage:(NSString *)input callback:(RCTResponseSenderBlock)callback) {    // Create NSURL from uri   NSURL *url = [[NSURL alloc] initWithString:input];    // Create an ALAssetsLibrary instance. This provides access to the   // videos and photos that are under the control of the Photos application.   //ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];    // Using the ALAssetsLibrary instance and our NSURL object open the image.   //[library assetForURL:url resultBlock:^(ALAsset *asset) {      // Create an ALAssetRepresentation object using our asset     // and turn it into a bitmap using the CGImageRef opaque type.     //CGImageRef imageRef = [asset thumbnail];      // Create UIImageJPEGRepresentation from CGImageRef    // NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);      // Convert to base64 encoded string     // NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0];      callback(@[url]);    //} failureBlock:^(NSError *error) {     //NSLog(@"that didn't work %@", error);   //}];    } @end 

1 Answers

Answers 1

I never used this module before, but it looks like it expects absolute path of the file, however you are providing a file uri.

How are you obtaining this file uri?

Try using react-native-get-real-path module to see if it helps, you can find it here: react-native-get-real-path

i.e. convert your file uri to obtain real path, and use that as path

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment