通过我的Android应用程序在Facebook上分享视频 [英] Share video on Facebook through my Android app

查看:148
本文介绍了通过我的Android应用程序在Facebook上分享视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


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



我通过我的应用程序张贴了墙,它的工作正常。现在我正在Facebook上分享一个视频。所以我尝试下面的代码。

  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 =从我的应用程序测试视频共享;
捆绑参数

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

读取字节方法是

  public byte [] readBytes(InputStream inputStream)throws IOException {
//这个动态扩展来读取您读取的字节
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream );

//这是每个重复的字节存储被覆盖
int bufferSize = 1024;
byte [] buffer = new byte [bufferSize];

//我们需要知道如何读取字节来将它们写入byteBuffer
int len = 0;
while((len = inputStream.read(buffer))!= -1){
byteBuffer.write(buffer,0,len);
}

//然后我们可以返回你的字节数组。
return byteBuffer.toByteArray();
}

SampleUploader类是



public class SampleUploadListener extends BaseRequestListener {

public void onComplete(final String response,final Object state){
try {
/ /处理这里的响应:(在后台线程中执行)
Log.d(Facebook-Example,Response:+ response.toString());
JSONObject json = Util.parseJson(response);
final String src = json.getString(src);

//然后将处理后的结果发回UI线程
//如果我们不这样做,将会生成运行时异常
//例如CalledFromWrongThreadException:只有创建视图层次结构的原始
//线程才能触及其视图。
Example1.this.runOnUiThread(new Runnable(){
public void run(){
mText.setText(Hello there,video has been uploaded on \\\
+ src);
}
});
}
catch(JSONException e){
Log.w(Facebook-example,JSON error in response);
}
catch(FacebookError e){
Log.w(Facebook-Example,Facebook Error:+ e.getMessage());
}
}
}

它不工作,在logcat中,它显示以下警告。

  08-23 11:29:32.364:WARN / Bundle(860)预期的byte [],但是值是一个java.lang.String。默认值< null>被归还
08-23 11:29:32.374:WARN / Bundle(860):尝试投射生成的内部异常:
08-23 11:29:32.374:WARN / Bundle(860):java.lang .ClassCastException:java.lang.String
08-23 11:29:32.374:WARN / Bundle(860):在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):键格式预期的byte [],但值是一个java.lang.String。默认值< null>被归还
08-23 11:29:32.396:WARN / Bundle(860):尝试投射生成的内部异常:
08-23 11:29:32.396:WARN / Bundle(860):java.lang .ClassCastException:java.lang.String
08-23 11:29:32.396:WARN / Bundle(860):在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):BufferedOutputStream构造函数中使用的默认缓冲区大小。如果需要8k缓冲区,最好是明确的。
08-23 11:29:33.905:WARN / Bundle(860):键消息预期字节[],但值为java.lang.String。默认值< null>被归还
08-23 11:29:33.905:WARN / Bundle(860):尝试投射生成的内部异常:
08-23 11:29:33.905:WARN / Bundle(860):java.lang .ClassCastException:java.lang.String
08-23 11:29:33.905:WARN / Bundle(860):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):键方法expect byte [],但值是java.lang.String。默认值< null>被归还
08-23 11:29:33.915:WARN / Bundle(860):尝试投射生成的内部异常:
08-23 11:29:33.915:WARN / Bundle(860):java.lang .ClassCastException:java.lang.String
08-23 11:29:33.915:WARN / Bundle(860):在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):键格式预期的字节[],但值是一个java.lang.String。默认值< null>被归还
08-23 11:29:33.935:WARN / Bundle(860):尝试投射生成的内部异常:
08-23 11:29:33.935:WARN / Bundle(860):java.lang .ClassCastException:java.lang.String
08-23 11:29:33.935:WARN / Bundle(860):在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上分享视频?

解决方案

我在Stack&Overflow中研究过,发现 中的答案是否可以通过Facebook SDK将SD卡上的视频上传到Facebook?


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

I want to share a video on Facebook though my Android app.

I posted a wall though my application, and it's working fine. Now I am trying to share a video on Facebook. So I tried below code.

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

The reading bytes method is

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

The SampleUploader class is

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

It is not working, and in logcat it shows the below warnings.

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)

How do I share a video on Facebook from my application?

解决方案

I researched in Stack Overflow and found an answer in Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?.

这篇关于通过我的Android应用程序在Facebook上分享视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆