我一直在寻找找到使用android和facebook sdk将图像+文本发布到Facebook的正确方法。我可以得到这个代码只发布一条消息,但当我尝试发布截图时,我发现它最终会出现空指针异常。我尝试过其他线程的许多不同的代码片段,但似乎没有一个对我有用。我能做的最好的是这段代码,它会将信息发布到我的墙上而不是照片。请告诉我这里我做错了什么。非常感谢。
View content = findViewById(R.id.row1);
Log.d("captureScreen", content.getId()+"");
byte[] byteArray=null;
Bitmap screenshot=null;
try {
if (content != null) {
int width = content.getWidth();
int height = content.getHeight();
screenshot = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
content.draw(new Canvas(screenshot));
Log.d("captureScreen", "success");
}
} catch (Exception e) {
Log.d("captureScreen", "Failed");
}
try{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
Log.d("byte array", "sucesss");
} catch(Exception e){
Log.d("byte array", "failed");
}
Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response.getGraphObject()
.getInnerJSONObject();
Log.d("onCompleted", "2 sucess");
String postId = null;
try {
postId = graphResponse.getString("id");
Log.d("onCompleted", "3 sucess");
} catch (JSONException e) {
Log.d("json failed", "failed");
Log.i(TAG, "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
} else {
CreateDialog("your post was a sucess", "posted");
}
}
};
Bundle params = new Bundle();
params.putString("message","tester");
//params.putString("method", "photos.upload");
//params.putByteArray("source", byteArray);
// params.putString("caption", "test Caption");
Request request = new Request(session, "me/photos", params,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
答案 0 :(得分:0)
在我的应用程序中,我使用以下代码在Facebook上分享照片+文本。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourbitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
params.putString("caption",
"This is screen shot from MyApp. Visit my application blah blah blah");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST",
new SampleUploadListener(), null);