3

我通過Gradle使用Facebook Android sdk 4.6.0Facebook分享視頻例外「ShareVideo必須參考Android設備上的視頻」

我試圖根據Sharing on Facebook guidenline配置Facebook後,從移動目錄上傳視頻,但在sharedialog.show調用後,我收到異常「ShareVideo必須引用設備上的視頻」。這個異常通過`onError(FacebookException異常)回調報告給我。

/**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 
        } 
       } 

回答

7

異常被拋出在這裏: 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())); 
} 

正如你所看到的,fb share sdk正在檢查uri是否有方案。 你的情況,當你從video.getPath創建uri時,scheme是null。你應該做的是從視頻文件創建uri:

Uri videoFileUri = Uri.fromFile(video);

+3

你的回答是完美的,代碼 從這個「Uri videoFileUri = Uri.parse(video.getPath());」的變化 「Uri videoFileUri = Uri.fromFile(video);」 –