Jig*_*300 5 android facebook image
似乎没有明确的参考.我正在创建一个Android应用程序,用户可以登录到FB.
我在FB网站上关注了本教程,该网站提供了一个从网址发布图片的示例: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();
}
}
Run Code Online (Sandbox Code Playgroud)
我能找到的所有例子都是处理Facebook类实例和AsyncFacebookRunner,它们都很沮丧.
此外,我从请求得到的错误响应是: HttpStatus:400,errorCode:100,errorType:GraphMethodException,errorMessage:不支持的方法,photos.upload
那么photos.upload更换是什么?请指教,代码示例将很棒,tnx.
如果您想上传照片,为什么不在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();
Run Code Online (Sandbox Code Playgroud)
李明让我走上了正确的轨道,但这是一个更完整的解决方案.这是经过测试和运作的.有两个要素:(1)创建回调以获取上传照片的URL; (2)上传照片的代码.这是两个部分的完整代码.照片的URL将加载到字符串变量中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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7841 次 |
最近记录: |