似乎没有明确的参考。 我正在创建一个用户可以登录FB的Android应用程序。
我关注this tutorial on FB site,其中提供了从网址发布图片的示例: postParams.putString(“picture”,“https:// image URL”);
但是,我想从我的项目上传到登录用户的时间轴上的本地PNG图像,该图像位于所有可重新绘制的文件夹中。
这是我的代码:
void publishStory()
{
Session session = Session.getActiveSession();
if (session != null)
{
Bundle postParams = new Bundle();
postParams.putString("name", "Name here.");
postParams.putString("caption", "Caption here.");
postParams.putString("description", "Description here.");
postParams.putString("link", "https://developers.facebook.com/android");
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bi = BitmapFactory.decodeResource(getResources(),R.drawable.logonew);
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
postParams.putString("method", "photos.upload");
postParams.putByteArray("picture", data);
Request.Callback callback = new Request.Callback()
{
public void onCompleted(Response response)
{
FacebookRequestError error = response.getError();
if (error != null)
Toast.makeText(_context , error.getErrorMessage(), Toast.LENGTH_SHORT).show();
else
Toast.makeText(_context, "Posted successful on your wall", Toast.LENGTH_SHORT).show();
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
我能找到的所有例子都是处理Facebook类实例和AsyncFacebookRunner,这些都是郁闷的。
此外,我从请求中得到的错误响应是: HttpStatus:400,errorCode:100,errorType:GraphMethodException,errorMessage:Unsupported method,photos.upload
那么photos.upload更换是什么?请指教,代码示例将很棒,tnx。
答案 0 :(得分:7)
如果您想上传照片,为什么不在Reqeust类中使用newUploadPhotoRequest? https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadPhotoRequest%28Session,%20Bitmap,%20Callback%29
Bitmap image = ... // get your bitmap somehow
Request.Callback callback = ... // create your callback
Request request = Request.newUploadPhotoRequest(session, image, callback);
request.executeAsync();
答案 1 :(得分:5)
李明让我走上了正确的道路,但这是一个更完整的解决方案。这是经过测试和运作的。有两个要素:(1)创建回调以获取上传照片的URL; (2)上传照片的代码。这是两个部分的完整代码。照片的网址将加载到字符串变量fbPhotoAddress
。
String fbPhotoAddress = null;
// Part 1: create callback to get URL of uploaded photo
Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
@Override
public void onCompleted(Response response) {
// safety check
if (isFinishing()) {
return;
}
if (response.getError() != null) { // [IF Failed Posting]
Log.d(logtag, "photo upload problem. Error="+response.getError() );
} // [ENDIF Failed Posting]
Object graphResponse = response.getGraphObject().getProperty("id");
if (graphResponse == null || !(graphResponse instanceof String) ||
TextUtils.isEmpty((String) graphResponse)) { // [IF Failed upload/no results]
Log.d(logtag, "failed photo upload/no response");
} else { // [ELSEIF successful upload]
fbPhotoAddress = "https://www.facebook.com/photo.php?fbid=" +graphResponse;
} // [ENDIF successful posting or not]
} // [END onCompleted]
};
//Part 2: upload the photo
Request request = Request.newUploadPhotoRequest(session, imageSelected, uploadPhotoRequestCallback);
request.executeAsync();