-1

我正在尝试将视频从我的厨房上传到 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();
}

首先我在日志猫中收到这样的警告

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)

我在 Google 中搜索,有人建议 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));
                }
            }

现在我没有收到任何警告任何错误,但我的视频没有上传请帮助我,我在这里感到震惊。

提前致谢

4

2 回答 2

0

几天前,我使用了这个stackoverflow 帖子来让视频上传正常工作。我将重新开始并完全按照该答案中的说明使其正常工作。我知道它有效,因为它就在今天对我有效。

于 2012-09-07T18:24:04.120 回答
0

Try this, it works for me.

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

        }
于 2014-10-15T10:40:00.373 回答