1

I'm writing an android app to upload and share a video on the facebook. My code is as below:

 public void doShareVideo(View view) {
        List<String> permissionNeeds = Arrays.asList("publish_actions","publish_pages");
        //this loginManager helps you eliminate adding a LoginButton to your UI
        LoginManager manager = LoginManager.getInstance();

        manager.logInWithPublishPermissions(this, permissionNeeds);

        Uri videoFileUri = Uri.parse("file:///storage/emulated/0/Movies/Untitled.mp4");
        ShareVideo video = new ShareVideo.Builder()
                .setLocalUrl(videoFileUri)
                .build();
        ShareVideoContent content = new ShareVideoContent.Builder()
                .setVideo(video)
                .setContentTitle("Video shared from my android apps")
                .build();

        ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                error.printStackTrace();
            }
        });
    }

But I get the error message when running:

10-10 02:57:40.714 19490-19490/? W/System.err: Error preparing share content: Permission denied
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.internal.ShareInternalUtility.invokeOnErrorCallback(ShareInternalUtility.java:529)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.internal.ShareInternalUtility.invokeCallbackWithError(ShareInternalUtility.java:98)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.internal.ShareInternalUtility.invokeCallbackWithException(ShareInternalUtility.java:90)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.ShareApi.shareVideoContent(ShareApi.java:417)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.ShareApi.share(ShareApi.java:186)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.ShareApi.share(ShareApi.java:79)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.anhnn.facebooklogin.ShareLinkActivity.doShareVideo(ShareLinkActivity.java:189)
10-10 02:57:40.714 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-10 02:57:40.714 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 02:57:40.714 19490-19490/? W/System.err:     at android.view.View$1.onClick(View.java:4015)
10-10 02:57:40.714 19490-19490/? W/System.err:     at android.view.View.performClick(View.java:4780)
10-10 02:57:40.714 19490-19490/? W/System.err:     at android.view.View$PerformClick.run(View.java:19866)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.os.Looper.loop(Looper.java:135)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
10-10 02:57:40.715 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-10 02:57:40.715 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 02:57:40.715 19490-19490/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-10 02:57:40.715 19490-19490/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-10 02:57:40.757 19490-19490/? W/art: Attempt to remove local handle scope entry from IRT, ignoring
10-10 02:57:40.758 19490-19490/? W/AwContents: onDetachedFromWindow called when already detached. Ignoring

I use Facebook SDK: com.facebook.android:facebook-android-sdk:4.6.0 Please help me and thank you very much.


To @Fyodor Volchyok, I already changed my code as your suggestion, but nothing happens to me. No log information or no error is showed in the LogCat after log-in the Facebook and approving the permissions successfully. My new code is as below:

public class ShareVideoActivity extends AppCompatActivity {

    private static final String LOG_TAG = "ShareVideo";
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_video);

        callbackManager = CallbackManager.Factory.create();
        LoginManager loginManager = LoginManager.getInstance();
        loginManager.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // it would be nice here to check loginResult.getRecentlyGrantedPermissions() for requested permissions
                        Log.d(LOG_TAG, "Success " + loginResult.getRecentlyGrantedPermissions().toString());
                        Uri videoFileUri = Uri.parse("file:///storage/emulated/0/Movies/Untitled.mp4");
                        ShareVideo video = new ShareVideo.Builder()
                                .setLocalUrl(videoFileUri)
                                .build();
                        ShareVideoContent content = new ShareVideoContent.Builder()
                                .setVideo(video)
                                .setContentTitle("Video shared from my android apps")
                                .build();

                        ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
                            @Override
                            public void onSuccess(Sharer.Result result) {

                            }

                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onError(FacebookException error) {
                                error.printStackTrace();
                            }
                        });
                    }

                    @Override
                    public void onCancel() {
                        // App code
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                    }
                });
        List<String> permissionNeeds = Arrays.asList("publish_actions", "publish_pages");
        loginManager.logInWithPublishPermissions(this, permissionNeeds);
    }
}

Do you have any new suggestion for me? Thanks in advance. (I can share Image and Link but encountering the problem with Video)

ChrisF
  • 134,786
  • 31
  • 255
  • 325

2 Answers2

1

It seems like you're trying to upload video when permissions hasn't been granted yet. Try the following:

  1. Set callbacks for LoginManager.
  2. Log in with LoginManager.
  3. (MOST IMPORTANT!) Wait for user to log in. You will be notified via callbacks.
  4. Check whether permissions granted.
  5. If so, upload.

Approximate result could look like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager,
    new FacebookCallback < LoginResult > () {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // it would be nice here to check loginResult.getRecentlyGrantedPermissions() for requested permissions

            Uri videoFileUri = Uri.parse("file:///storage/emulated/0/Movies/Untitled.mp4");
            ShareVideo video = new ShareVideo.Builder()
                .setLocalUrl(videoFileUri)
                .build();
            ShareVideoContent content = new ShareVideoContent.Builder()
                .setVideo(video)
                .setContentTitle("Video shared from my android apps")
                .build();

            ShareApi.share(content, new FacebookCallback < Sharer.Result > () {
                @Override
                public void onSuccess(Sharer.Result result) {

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {
                    error.printStackTrace();
                }
            });
        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code   
        }
    });
    LoginManager.getInstance().logInWithPublishPermissions(this, permissionNeeds);
}

And yes, this code could and should be improved, but the whole idea is here.

Fyodor Volchyok
  • 5,610
  • 4
  • 28
  • 45
0

The reason is that uploading video to facebook requires a permission called "upload_video", if this permission is not granted by user, you can't upload the video.

Here is the example code to ensure the permission:

//Path to the video, Ex: path = Environment.getExternalStorageDirectory() + File.separator + "myVideo.mp4";
    String path;
    //get the current active facebook session
    Session session = Session.getActiveSession();
    //If the session is open
    if(session.isOpened()) {
        //Get the list of permissions associated with the session
        List<String> permissions = session.getPermissions();
        //if the session does not have video_upload permission
        if(!permissions.contains("video_upload")) {
            //Get the permission from user to upload the video to facebook
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(this, Arrays.asList("video_upload"));
            session.requestNewReadPermissions(newPermissionsRequest);
        }


        //Create a new file for the video 
        File file = new File(path);
        try {
            //create a new request to upload video to the facebook
            Request videoRequest = Request.newUploadVideoRequest(session, file, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {

                    if(response.getError()==null)
                    {
                        Toast.makeText(MainActivity.this, "video shared successfully", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

            //Execute the request in a separate thread
            videoRequest.executeAsync();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //Session is not open
    else {
        Toast.makeText(getApplicationContext(), "Please login to facebook first", Toast.LENGTH_SHORT).show();
    }

Reference:

Facebook for android video upload - works with one app and not with another

Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

Note: the video should less than 12MB according to the FB's rule

Community
  • 1
  • 1
SHICONG CAO
  • 219
  • 3
  • 7