从Android设备上传Facebook墙上的视频?

时间:2012-09-07 06:58:52

标签: android facebook facebook-graph-api

我正在尝试将视频从我的厨房上传到Facebook墙,这里是我的代码

 byte[] data = null;
    String dataPath = "/mnt/sdcard/DCIM/Camera/video-2012-09-03-13-34-19.mp4";
    Bundle param;
   //video extension
    data Name = ".mp4"
    facebook = new Facebook(FB_APP_ID);
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    InputStream is = null;
    try {
        is = new FileInputStream(dataPath);
        data = readBytes(is);
        param = new Bundle();
        param.putString("message", "hello guys");
        param.putString("filename", data Name);
        param.putByteArray("video", data);
        mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
    }
    catch (FileNotFoundException e) {
       e.printStackTrace();
    }
    catch (IOException e) {
       e.printStackTrace();
    }

...................

public byte[] readBytes(InputStream inputStream) throws IOException {
    // This dynamically extends to take the bytes you read.
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

    // This is storage overwritten on each iteration with bytes.
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    // We need to know how may bytes were read to write them to the byteBuffer.
    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }

    // And then we can return your byte array.
    return byteBuffer.toByteArray();
}

首先我在log cat中收到这样的警告

W/Bundle  (  774): Attempt to cast generated internal exception:
W/Bundle  (  774): java.lang.ClassCastException: java.lang.String
W/Bundle  (  774):  at android.os.Bundle.getByteArray(Bundle.java:1305)
W/Bundle  (  774):  at com.facebook.android.Util.openUrl(Util.java:172)
W/Bundle  (  774):  at com.facebook.android.Facebook.request(Facebook.java:751)
W/Bundle  (  774):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
W/Bundle  (  774): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
W/Bundle  (  774): Attempt to cast generated internal exception:
W/Bundle  (  774): java.lang.ClassCastException: java.lang.String
W/Bundle  (  774):  at android.os.Bundle.getByteArray(Bundle.java:1305)
W/Bundle  (  774):  at com.facebook.android.Util.openUrl(Util.java:172)
W/Bundle  (  774):  at com.facebook.android.Facebook.request(Facebook.java:751)

我在谷歌搜索有些人认为Facebook API中存在错误,因为他们建议我修改我的Util.java中的一些行

for (String key : parameters.keySet()) {
            if (parameters.get(key) instanceof byte[]) {

                continue;
            }

            sb.append("Content-Disposition: form-data; name=\"" + key
                    + "\"\r\n\r\n" + parameters.getString(key));
            sb.append("\r\n" + "--" + boundary + "\r\n");
        }

        return sb.toString();
    }

for (String key : params.keySet()) {
                if (params.get(key) instanceof byte[]) {
                    dataparams.putByteArray(key, params.getByteArray(key));
                }
            }

现在我没有收到任何警告任何错误,但我的视频没有上传请帮助我,我被击中了。

提前致谢

2 个答案:

答案 0 :(得分:0)

我几天前使用此stackoverflow post来让视频上传工作正常。我会重新开始并完全遵循该答案中的说明,以使其发挥作用。我知道它有效,因为它只适用于我今天。

答案 1 :(得分:0)

试试这个,它对我有用。

File file=new File("your video file name");
        try {
            Request request = Request.newUploadVideoRequest(session, file, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {
                    // TODO Auto-generated method stub

                    if(response.getError()==null)
                    {
                        Log.e("response",response.toString());
                        Toast.makeText(MainActivity.this, "Video Shared Successfully", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Log.e("error",response.getError().getErrorMessage());
                        Toast.makeText(MainActivity.this, response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

            request.executeAsync();
        } catch (Exception e) {
            e.printStackTrace();

        }