gpt4 book ai didi

react-native - React Native - 每封邮件发送照片

转载 作者:行者123 更新时间:2023-12-03 14:43:02 26 4
gpt4 key购买 nike

我正在尝试通过邮件发送最近在应用程序中捕获的照片,但遇到以下错误:

(对于我正在使用此模块的邮件功能:var Mailer = require('NativeModules').RNMail;
我试图在此模块的帮助下通过邮件发送照片并收到以下错误:

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
)

这是调用代码:
.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');
}
});
})

路径无效吗?或者可能是别的东西。

编辑

我正在使用此模块 react-native-camera 获取文件路径
像这样:

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

元素:
<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>

更新2

在包含 obj-c file 之后对于 uri 到路径的转换,我现在收到以下错误:
ExceptionsManager.js:76 JSON value '<null>' of type NSNull cannot be converted to NSString

我是否从以下代码中“删除”了错误的行? :/

obj-c 文件内容:
#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

最佳答案

------ 编辑答案如下 ------
好的,所以我终于有了一台 Mac,并且能够更详细地研究这个问题。

这是我为 Android 和 iOS 找到的。

假设您正在使用 react-native-camera连同 react-native-mail

- 1:绝对路径

添加房产 captureTarget={Camera.constants.CaptureTarget.disk}Camera像这样的组件:

<Camera
captureTarget={Camera.constants.CaptureTarget.disk}
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={Camera.constants.Aspect.fill}>
<Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>
</Camera>

现在相机组件应该返回 绝对 文件路径而不是 uri。

所以对于 安卓您应该会看到类似这样的内容:“ file:///storage/emulated/0/Pictures/RCTCameraModule/IMG_20160730_060652.jpg ”而不是:“ content://media/external/images/媒体/86 "和 iOS 你应该得到这样的东西:“ /Users/anton/Library/Developer/CoreSimulator/Devices/9A15F203-9A58-41C5-A4FC-EA25FAAE92BD/data/Containers/Data/Application/79FF93F9-BA89-4F4C-8809- 277BEECD447D/Documents/EFFF0ECE-4063-4FE5-984E-E76506788350.jpg “而不是:” assets-library://asset/asset.JPG?id=0058FA4A-268F-408A-9150-017A3DA368D2 JPG "

- 2:陷阱

iOS :

如果 Apple 的 MFMailComposeViewController 崩溃并且您看到以下错误消息:

enter image description here

这很可能是因为您在 上运行应用程序。 iOS 9 模拟器。解决方案:要么在真机上测试应用,要么下载旧版模拟器,例如 iOS 8.4。有关此问题的更多信息,请访问 here

安卓 :

在撰写本文时,还没有对 Android 的附件支持。

解决方案:(已经做PR添加这个功能,但是如果你等不及了)

将以下代码添加到文件 RNMAILModule.java
if (options.hasKey("attachment") && !options.isNull("attachment")) {
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(options.getMap("attachment").getString("path")));
}

当 Android 和 iOS 都工作时,你应该有这样的东西:

enter image description here
enter image description here

这是工作代码:
var Mailer = require('NativeModules').RNMail;
import Camera from 'react-native-camera';

import React, {Component} from 'react';
import
{
View,
TouchableHighlight,
Text,
StyleSheet,
Dimensions,
CameraRoll
}
from 'react-native';

const styles = StyleSheet.create({
container: {
flex: 1
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
height: Dimensions.get('window').height,
width: Dimensions.get('window').width
},
capture: {
flex: 0,
backgroundColor: '#fff',
borderRadius: 5,
color: '#000',
padding: 10,
margin: 40
}
});

class SendPhoto extends Component {
takePicture() {
this.camera.capture()
.then((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');
}
})
})
.catch(err => console.error(err));
}

render() {
return(
<View>
<View style={styles.container}>
<Camera
captureTarget={Camera.constants.CaptureTarget.disk}
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={Camera.constants.Aspect.fill}>
<Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>
</Camera>
</View>
</View>
);
}
}
export default SendPhoto;

------ 下面是旧答案 ------
我以前从未使用过此模块,但看起来它需要文件的绝对路径,但是您提供的是文件 uri。

你是如何获得这个文件uri的?

尝试使用 react-native-get-real-path 模块看看它是否有帮助,你可以在这里找到它: react-native-get-real-path

即转换您的文件 uri 以获得真实路径,并将其用作 路径

关于react-native - React Native - 每封邮件发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38141637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com