android - 如何使用渲染操作在 React-native-Gifted-chat 中发送图像/视频和语音消息?
问题描述
我想在使用 React-native-gifted-chat 和 Firebase 开发的聊天应用程序中发送图像/视频,如何为他们创建操作并调用该操作以在 firebase 中上传并发送图像/视频?
这是我的代码。
handleSendImage = () => {
console.log("handleSendImage");
};
renderActions(props) {
return (
<Actions
{...props}
// containerStyle={{
// width: 70,
// }}
icon={() => (
<Icon
name={"camera"}
size={30}
color={colors.btnBg}
font={"FontAwesome"}
onPress={this.handleSendImage}
/>
)}
onSend={(args) => console.log(args)}
/>
);
}
<GiftedChat
placeholder={"Hey!"}
alwaysShowSend
messages={messages}
onSend={(newMessage) => this.onSend(this.chatID(), newMessage)}
renderInputToolbar={this.renderInputToolbar}
renderActions={this.renderActions}
user={{
_id: senderId,
name: senderName,
}}
/>
如何单击特定操作并分别发送语音和图像/视频?
解决方案
天才聊天本身具有renderActions属性,因此只需创建自定义操作即可上传图像/视频和语音。
在这里,我附上了用于上传 PDF/Doc 文件等文档的代码。
要上传图像/视频,您只需要更改该包而不是我使用过的文档选择器
const renderActions = (props) => {
return (
<Actions
{...props}
options={{
['Document']: async (props) => {
try {
const result = await DocumentPicker.pick({
type: [DocumentPicker.types.pdf],
});
console.log("image file",result)
} catch (e) {
if (DocumentPicker.isCancel(e)) {
console.log("User cancelled!")
} else {
throw e;
}
}
},
Cancel: (props) => { console.log("Cancel") }
}}
onSend={args => console.log(args)}
/>
)
};
有天赋的聊天组件
<GiftedChat
messages={messages}
showAvatarForEveryMessage={true}
onSend={messages => onSend(messages)}
renderActions={() => renderActions()}
user={{
_id: 2,
name: 'React Native',
avatar: 'https://placeimg.com/140/140/any',
}}
renderCustomView={renderCustomView}
/>
推荐阅读
- android - 如何在按钮单击时在自定义视图中绘制一个圆圈
- c - c 中指向 r 值的指针赋值
- angular - 构建时间从 3 分钟到大约 70 分钟 - 使用 Angular7 升级
- javascript - Expo/React Native Fetch API 不返回文本/纯文本数据
- c++ - 找到一种方法将匹配的键和值从映射推回向量
- ruby-on-rails - Rspec:控制器操作未重新路由
- vba - 保存联系人后创建约会
- kubernetes - VMSS - LoadBalancer 默认情况下未关联的后端池
- mql5 - 关于 MQL5,如何关闭交易并进入交易
- umbraco - 在 Umbraco 8 中访问图像 URL