【问题标题】:How to upload (post) a video to Facebook using an Android app?如何使用 Android 应用将视频上传(发布)到 Facebook?
【发布时间】:2014-08-09 20:57:06
【问题描述】:

试图有一个简单的视频上传应用程序,发现这个答案 eclipse 说那里的许多功能已被弃用。

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

我使用的是最新的 Facebook SDK(3.16 版)。

是否有从 SD 卡中获取视频并将其发布到 Facebook 墙上的简单示例?

【问题讨论】:

  • 我错过了什么吗?我认为这应该很容易

标签: android facebook post video share


【解决方案1】:

这就是我所做的。 首先是你的回调,

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();
            }
        }
    };

然后,看看你是否已经拥有 publish_actions 权限,

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

        return true;
    }

然后把这个方法放在你的 onClick() 方法中

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();

    }

我们的 publish_action 权限,

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

那么这是我的 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.");
        }

    }

当然,出于安全原因,我编辑了很多代码。让我知道是否缺少某些内容和损坏。

我正在使用Facebook's latest SDK 3.17 as of August 7, 2014

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多