react-native-fbsdk无法分享视频

时间:2019-03-15 07:19:14

标签: react-native

const shareContent = {
    contentType: 'video',
    video: {
        localUrl: 'response.uri'
    },
};
ShareDialog.show(shareContent)

报错:ShareDialog遇到错误

1 个答案:

答案 0 :(得分:0)

import React, {Component} from 'react';
import { Text, TouchableOpacity, NativeModules, SafeAreaView, StyleSheet} from "react-native";
import ImagePicker from 'react-native-image-picker';
const ShareDialog = NativeModules.FBShareDialog

export default class Facebook extends Component {
    render() {
        return (
            <SafeAreaView style={{flex: 1, justifyContent: 'center', alignItems: 'center',}}>
                <TouchableOpacity onPress={() => {
                    this.shareLinkWithShareDialog()
                }} style={[styles.avatar, styles.avatarContainer, { marginBottom: 30 }]}>
                    <Text>分享链接</Text>
                </TouchableOpacity>

                <TouchableOpacity onPress={() => {
                    this.selectPhotoTapped()
                }} style={[styles.avatar, styles.avatarContainer, { marginBottom: 30 }]}>
                    <Text>分享图片</Text>
                </TouchableOpacity>

                <TouchableOpacity onPress={() => {
                    this.selectVideoTapped()
                }} style={[styles.avatar, styles.avatarContainer, { marginBottom: 30 }]}>
                    <Text>分享视频</Text>
                </TouchableOpacity>
            </SafeAreaView>
        )
    }

    // 分享链接
    shareLinkWithShareDialog() {
        const shareContent = {
            contentType: 'link',
            contentUrl: "www.facebook.com",
            quote: "假装是个标题",
        };
        this.shareWithShareDialog(shareContent)
    }


    // 分享图片
    selectPhotoTapped() {
        var photoOptions = {
            //底部弹出框选项
            title: '请选择',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍照',
            chooseFromLibraryButtonTitle: '选择相册',
            quality: 0.75,
            allowsEditing: true,
            noData: false,
            storageOptions: {
                skipBackup: true,
                path: 'images'
            }
        }

        ImagePicker.showImagePicker(photoOptions, (response) => {
            console.log('Response = ', response);

            if (response.didCancel) {
                console.log('User cancelled photo picker');
            }
            else if (response.error) {
                console.log('ImagePicker Error: ', response.error);
            }
            else if (response.customButton) {
                console.log('User tapped custom button: ', response.customButton);
            }
            else {
                const shareContent = {
                    contentType: 'photo',
                    photos: [
                        {
                            imageUrl: response.uri,
                            userGenerated: false,
                            caption: "Hello World"
                        }
                    ]
                };
                this.shareWithShareDialog(shareContent)
            }
        });
    }

    // 分享视频
    selectVideoTapped() {
        var videoOptions = {
            title: '选择视频',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '录制视频',
            chooseFromLibraryButtonTitle: '选择视频',
            mediaType: 'video',
            videoQuality: 'medium'
        }

        ImagePicker.showImagePicker(videoOptions, (response) => {
            console.log('Response = ', response);

            if (response.didCancel) {
                console.log('User cancelled photo picker');
            }
            else if (response.error) {
                console.log('ImagePicker Error: ', response.error);
            }
            else if (response.customButton) {
                console.log('User tapped custom button: ', response.customButton);
            }
            else {
                console.log('视频选择成功', response.uri)
                const videoUri = response.uri
                const shareContent = {
                    contentType: 'video',
                    video: {
                        localUrl: videoUri
                    },
                };
                ShareDialog.show(shareContent)
            }
        });
    }

    // 分享结构
    shareWithShareDialog(shareContent: any) {
        ShareDialog.canShow(shareContent).then(
            function (canShow) {
                console.log('canShow = ', canShow)
                if (canShow) { // vedio: canshow = false
                    return ShareDialog.show(shareContent); //  video Error: ShareDialog encounters error
                }
            }
        ).then(
            function (result) {
                if (result.isCancelled) {
                    console.log('Share cancelled');
                } else {
                    console.log('Share success with postId: '
                        + result.postId);
                }
            },
            function (error) {
                console.log('Share fail with error: ' + error);
            }
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF'
    },
    avatarContainer: {
        borderColor: '#9B9B9B',
        borderWidth: 1,
        justifyContent: 'center',
        alignItems: 'center'
    },
    avatar: {
        borderRadius: 50,
        width: 100,
        height: 100,
        marginTop: 30
    }

});

这是我的代码,分享链接和照片都OK,分享视频就会报错ShareDialog遇到错误

我不知道是视频路径的问题还是其他什么问题。