0

Trying to have a simple video upload app, found this answer which eclipse says many of the functions there are deprecated.

https://stackoverflow.com/a/6924732/1525654

I'm using the latest Facebook SDK (version 3.16).

Is there a simple example for taking a video from the SD card and post it onto a Facebook's wall ?

Community
  • 1
  • 1
Miko Diko
  • 944
  • 1
  • 13
  • 33

1 Answers1

0

Here's what I did. First your callback,

private Request.Callback requestCallback = new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            if(response.getError() == null) {
                Toast.makeText(YourActivity.this, "Posted to Facebook.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(YourActivity.this, "Post to Facebook failed.", Toast.LENGTH_SHORT).show();
            }
        }
    };

Then, see if you have the publish_actions permission already,

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
        for(String string : subset) {
            if(!superset.contains(string)) {
                return false;
            }
        }

        return true;
    }

Then put this method inside your onClick() method

public void uploadVideoToFb(String caption) {
        // Get Facebook's active session.
        Session session = Session.getActiveSession();

        /*
         * Check published permissions first.
         */
        List<String> permissionList = session.getPermissions();
        if(!isSubsetOf(FacebookFragment.PERMISSIONS, permissionList)) {
            /*pendingPublishReauthorization = true;*/
            /*
             * Set additional permission requests to be able
             * to publish on the Facebook feed.
             * Inside PERMISSIONS is just "publish_actions".
             */
            Session.NewPermissionsRequest newPermissionRequest = new Session.NewPermissionsRequest(this, FacebookFragment.PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionRequest);
            return;
        }

        new TaskDownloadAndPostToFb(this, caption).execute();

    }

Our publish_action permission,

public static final List<String> PERMISSIONS = Arrays.asList("publish_actions");

Then here's my AsyncTask,

@Override
    public void onPostExecute(Integer result) {
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + filename;
        File videoFile = new File(baseDir);
        if(videoFile.exists()) {
            Session session = Session.getActiveSession();
            if(session != null) {
                try {
                    Request request = Request.newUploadVideoRequest(session, videoFileLocal, requestCallback);
                    /*
                     * Take note of where to get the reference
                     of your bundle, it should always be
                     request.getParameters()
                     */
                    Bundle params = request.getParameters();
                    params.putString("description", this.caption);
                    request.setParameters(params);

                    request.executeAsync();

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

        } else {
            Log.i(TAG, "Video not found.");
        }

    }

Also, I have edited a lot of my code, of course, for security reasons. Let me know if something is lacking and broken.

I'm using Facebook's latest SDK 3.17 as of August 7, 2014

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52