Search code examples
androidfacebookfacebook-sharerfacebook-sharefacebook-exception

Facebook Share video Exception "ShareVideo must reference a video that is on the device" on Android


I am using Facebook Android sdk 4.6.0 through Gradle.

I am trying to upload video from mobile directory after configuring facebook according to Sharing on Facebook guidenline, but i am getting exception "ShareVideo must reference a video that is on the device" after sharedialog.show called. The exception is reported to me by callback on `onError(FacebookException exception).

/**first checking if file exist than execute code, file exits and code execute but after executing callback with exception "Share Video must reference a video that is on the device" occurs
 **/      private void shareOnFacebook() {
                    File dir = new File(Environment.getExternalStorageDirectory(),
                            "directory");
                    File video = new File(dir, "Video.mp4");
                    if (video.exists()) {//if video file exist
                        Uri videoFileUri = Uri.parse(video.getPath());
                        ShareVideo sv = new ShareVideo.Builder()
                                .setLocalUrl(videoFileUri)
                                .build();
                        ShareVideoContent content = new ShareVideoContent.Builder()
                                .setVideo(sv)
                                .build();
                        shareDialog.show(content); //show facebook sharing screen with video
                    }
                }

Solution

  • The exception is thrown here: https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/share/internal/ShareContentValidation.java;

     if (!Utility.isContentUri(localUri) && !Utility.isFileUri(localUri)) {
    throw new FacebookException("ShareVideo must reference a video that is on the device");}
    
    public static boolean isContentUri(final Uri uri) {
    return (uri != null) && ("content".equalsIgnoreCase(uri.getScheme()));
    }
    
    public static boolean isFileUri(final Uri uri) {
    return (uri != null) && ("file".equalsIgnoreCase(uri.getScheme())); 
    }
    

    As you can see, the fb share sdk is checking if uri has a scheme. In your case, when you create uri from video.getPath, scheme is null. What you should do is create uri from video file:

    Uri videoFileUri = Uri.fromFile(video);