首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过我的Android应用程序在Facebook上分享视频

通过我的Android应用程序在Facebook上分享视频
EN

Stack Overflow用户
提问于 2011-08-23 06:03:59
回答 1查看 8.1K关注 0票数 2

可能重复:

Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

我想通过我的Android应用程序在Facebook上分享一段视频。

我通过我的申请贴了一面墙,它运行得很好。现在我正尝试在Facebook上分享一段视频。所以我尝试了下面的代码。

代码语言:javascript
复制
    Facebook facebook;

    String FB_APP_ID=APP_ID;

    byte[] data = null;
    Uri uri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"filename.mp4"));
    String dataPath = uri.getPath();
    String dataMsg = "Testing video sharing from my app";
    Bundle param;

    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", dataMsg);
        param.putByteArray("video", data);
        mAsyncRunner.request("me/videos", param, "POST", new SampleUploadListener(), null);
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

读取字节方法是

代码语言:javascript
复制
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();
}

SampleUploader类是

代码语言:javascript
复制
public class SampleUploadListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {
        try {
            // process the response here: (executed in background thread)
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            final String src = json.getString("src");

            // then post the processed result back to the UI thread
            // if we do not do this, an runtime exception will be generated
            // e.g. "CalledFromWrongThreadException: Only the original
            // thread that created a view hierarchy can touch its views."
            Example1.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText("Hello there, video has been uploaded at \n" + src);
                }
            });
        }
        catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        }
        catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
    }
}

它不起作用,在logcat中它显示了以下警告。

代码语言:javascript
复制
08-23 11:29:32.364: WARN/Bundle(860): Key message expected byte[] but value was a java.lang.String.  The default value <null> was returned.
08-23 11:29:32.374: WARN/Bundle(860): Attempt to cast generated internal exception:
08-23 11:29:32.374: WARN/Bundle(860): java.lang.ClassCastException: java.lang.String
08-23 11:29:32.374: WARN/Bundle(860):     at android.os.Bundle.getByteArray(Bundle.java:1305)
08-23 11:29:32.374: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.openUrl(Util.java:161)
08-23 11:29:32.374: WARN/Bundle(860):     at com.stellent.Tout.facebook.Facebook.request(Facebook.java:559)
08-23 11:29:32.374: WARN/Bundle(860):     at com.stellent.Tout.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
08-23 11:29:32.386: WARN/Bundle(860): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
08-23 11:29:32.396: WARN/Bundle(860): Attempt to cast generated internal exception:
08-23 11:29:32.396: WARN/Bundle(860): java.lang.ClassCastException: java.lang.String
08-23 11:29:32.396: WARN/Bundle(860):     at android.os.Bundle.getByteArray(Bundle.java:1305)
08-23 11:29:32.396: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.openUrl(Util.java:161)
08-23 11:29:32.396: WARN/Bundle(860):     at com.stellent.Tout.facebook.Facebook.request(Facebook.java:559)
08-23 11:29:32.396: WARN/Bundle(860):     at com.stellent.Tout.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
08-23 11:29:33.896: INFO/global(860): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
08-23 11:29:33.905: WARN/Bundle(860): Key message expected byte[] but value was a java.lang.String.  The default value <null> was returned.
08-23 11:29:33.905: WARN/Bundle(860): Attempt to cast generated internal exception:
08-23 11:29:33.905: WARN/Bundle(860): java.lang.ClassCastException: java.lang.String
08-23 11:29:33.905: WARN/Bundle(860):     at android.os.Bundle.getByteArray(Bundle.java:1305)
08-23 11:29:33.905: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.encodePostBody(Util.java:63)
08-23 11:29:33.905: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.openUrl(Util.java:188)
08-23 11:29:33.905: WARN/Bundle(860):     at com.stellent.Tout.facebook.Facebook.request(Facebook.java:559)
08-23 11:29:33.905: WARN/Bundle(860):     at com.stellent.Tout.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
08-23 11:29:33.915: WARN/Bundle(860): Key method expected byte[] but value was a java.lang.String.  The default value <null> was returned.
08-23 11:29:33.915: WARN/Bundle(860): Attempt to cast generated internal exception:
08-23 11:29:33.915: WARN/Bundle(860): java.lang.ClassCastException: java.lang.String
08-23 11:29:33.915: WARN/Bundle(860):     at android.os.Bundle.getByteArray(Bundle.java:1305)
08-23 11:29:33.915: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.encodePostBody(Util.java:63)
08-23 11:29:33.915: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.openUrl(Util.java:188)
08-23 11:29:33.915: WARN/Bundle(860):     at com.stellent.Tout.facebook.Facebook.request(Facebook.java:559)
08-23 11:29:33.915: WARN/Bundle(860):     at com.stellent.Tout.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
08-23 11:29:33.935: WARN/Bundle(860): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
08-23 11:29:33.935: WARN/Bundle(860): Attempt to cast generated internal exception:
08-23 11:29:33.935: WARN/Bundle(860): java.lang.ClassCastException: java.lang.String
08-23 11:29:33.935: WARN/Bundle(860):     at android.os.Bundle.getByteArray(Bundle.java:1305)
08-23 11:29:33.935: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.encodePostBody(Util.java:63)
08-23 11:29:33.935: WARN/Bundle(860):     at com.stellent.Tout.facebook.Util.openUrl(Util.java:188)
08-23 11:29:33.935: WARN/Bundle(860):     at com.stellent.Tout.facebook.Facebook.request(Facebook.java:559)
08-23 11:29:33.935: WARN/Bundle(860):     at com.stellent.Tout.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)

如何在Facebook上分享我的应用程序中的视频?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-27 07:32:32

我对堆栈溢出进行了研究,发现了an answer in

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7156886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档