使用fb sdk在Facebook墙上发布视频

时间:2012-12-27 14:09:40

标签: android facebook facebook-graph-api

我在log cat中收到错误

  

{响应:responseCode:400,graphObject:null,错误:{HttpStatus:   400,errorCode:353,errorType:OAuthException,errorMessage:(#353)   缺少视频文件},isFromCache:false}

使用以下方法

 private void postVideo() {         
        try {

            File file = new File(videoPath);

            ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            Bundle parameters = new Bundle(1);
            parameters.putParcelable(file.getName(), descriptor);

            new Request(Session.getActiveSession(), "me/videos", parameters, HttpMethod.POST, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {                        
                    Constants.showLog(TAG, "In Response " + response.getGraphObject().getProperty("id"));       

                    JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                        Constants.showLog(TAG, "In Response id " + postId);
                    } catch (JSONException e) {
                        Constants.showLog(TAG,"JSON error "+ e.getMessage());
                    }

                }
            }).executeAsync();


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

    }

请帮助

2 个答案:

答案 0 :(得分:3)

我发现您使用的是Android SDK 3.0,我们提供了一种上传视频的简单方法。以下是我用于将视频上传到Facebook的代码:

Request.Callback callback5 = new Request.Callback() {
    public void onCompleted(Response response) {    
        // response will have an id if successful
    }
};
File tempFile;
try {
    tempFile = Util.createTempFileFromAsset(getApplicationContext(), "video.mp4");
    Request request5 = Request.newUploadVideoRequest(session,
                    tempFile, callback5);
    RequestAsyncTask task5 = new RequestAsyncTask(request5);
    task5.execute();
} catch (IOException e) {
    Log.e(Util.TAG, "failed to create temp file");
    e.printStackTrace();
}

以及createTempFileFromAsset的代码:

public static File createTempFileFromAsset(Context context, String assetPath) 
    throws IOException {
    InputStream inputStream = null;
    FileOutputStream outStream = null;

    try {
        AssetManager assets = context.getResources().getAssets();
        inputStream = assets.open(assetPath);

        File outputDir = context.getCacheDir(); // context being the
                                                // Activity pointer
        File outputFile = File.createTempFile("prefix", assetPath,
                outputDir);
        outStream = new FileOutputStream(outputFile);

        final int bufferSize = 1024 * 2;
        byte[] buffer = new byte[bufferSize];
        int n = 0;
        while ((n = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, n);
        }

        return outputFile;
    } finally {
        Utility.closeQuietly(outStream);
        Utility.closeQuietly(inputStream);
    }
}

答案 1 :(得分:0)

选择文件onActivityResult ....

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

        case RESULT_LOAD_IMAGE:
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                         filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                SubmitPost.this.file = new File(SubmitPost.this.file, picturePath);

//              MimeTypeMap mime = MimeTypeMap.getSingleton();
//              String type = mime.getMimeTypeFromExtension(ext);

                cursor.close();
            }
            break;
}

并使用

if (!postParams.containsKey(MIGRATION_BUNDLE_PARAM)) {
                            postParams.putString(MIGRATION_BUNDLE_PARAM, FbSdkVersion.MIGRATION_BUNDLE);
                        }

ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            Bundle parameters = new Bundle(1);
            parameters.putParcelable(file.getName(), descriptor);