[英]Share image on whatsapp from react native android app
I am currently working on react-native photo sharing app for Android. Used native share method but it only share message and title.我目前正在为 Android 开发 react-native 照片共享应用程序。使用本机共享方法,但它只共享消息和标题。 No options to share an image.没有共享图像的选项。
Looking after so many questions here couldn't find any straight forward way.在这里寻找这么多问题找不到任何直接的方法。
Please provide help.请提供帮助。
This is the message I am getting Share awesome status on whatsapp using Khela #imageurl.这是我收到的消息 Share awesome status on whatsapp using Khela #imageurl。 Download #urltoplaystore下载#urltoplaystore
To share any image in React Native you are right you need to use the Share
from react-native
library itself, and you were wondering what is needed for an image, the answer it's really simple, you just need to use a Base64 image. 在React Native中共享任何图像都是正确的,您需要使用react-native
库本身的Share
,并且您想知道图像需要什么,答案很简单,您只需要使用Base64图像即可。
Check it out a working snack: snack.expo.io/@abranhe/share-image 签出一份工作小吃:快餐.expo.io / @ abranhe / share-image
Wrap the code: 包装代码:
import * as React from 'react';
import {
Text,
View,
StyleSheet,
Image,
Share,
TouchableOpacity,
} from 'react-native';
export default class App extends React.Component {
state = {
cat: 'data:image/jpeg;base64,some-encoded-stuff;
};
handleSharePress = () => {
Share.share({
title: 'Share',
message: 'My amazing cat 😻',
url: this.state.cat,
});
};
render() {
return (
<View style={styles.container}>
<Image source={{ uri: this.state.cat }} style={styles.img} />
<TouchableOpacity onPress={this.handleSharePress}>
<Text>Share Image</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-around',
alignItems: 'center',
},
img: {
width: 200,
height: 300,
},
});
If you want to try something else, probably complex, I recommend to check out the react-native-share
library from the React Native Community. 如果您想尝试其他可能很复杂的事情,我建议您从React Native Community中检出react-native-share
库。
import Icon from 'react-native-vector-icons/Feather';
import Share from 'react-native-share';
import RNFetchBlob from 'rn-fetch-blob';
import React, {Component} from 'react';
const fs = RNFetchBlob.fs;
class ProductDetail extends Component {
constructor(props) {
super(props);
this.state = {};
}
shareTheProductDetails(imagesPath) {
let {productDetails} = this.state;
let imagePath = null;
RNFetchBlob.config({
fileCache: true,
})
.fetch('GET', imagesPath.image)
// the image is now dowloaded to device's storage
.then((resp) => {
// the image path you can use it directly with Image component
imagePath = resp.path();
return resp.readFile('base64');
})
.then((base64Data) => {
// here's base64 encoded image
var imageUrl = 'data:image/png;base64,' + base64Data;
let shareImage = {
title: productDetails.product_name, //string
message:
'Description ' +
productDetails.product_description +
' http://beparr.com/', //string
url: imageUrl,
// urls: [imageUrl, imageUrl], // eg.'http://img.gemejo.com/product/8c/099/cf53b3a6008136ef0882197d5f5.jpg',
};
Share.open(shareImage)
.then((res) => {
console.log(res);
})
.catch((err) => {
err && console.log(err);
});
// remove the file from storage
return fs.unlink(imagePath);
});
}
render() {
return (
<TouchableOpacity
style={{
borderWidth: 0,
left:(5),
top:(2),
}}
onPress={() =>
this.shareTheProductDetails(images)
}>
<Icon
style={{
left: moderateScale(10),
}}
name="share-2"
color={colors.colorBlack}
size={(20)}
/>
</TouchableOpacity>
)}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.