安卓 Facebook SDK 3.0 上传本地图片

5
这似乎还没有一个清晰的参考指南。我正在创建一个用户可以登录FB的Android应用程序。
我按照FB网站上这个教程的例子,发布了一个来自Web URL的图片: postParams.putString("picture", "https:// image URL");
但是,我想上传一个位于所有res-drawable文件夹中的本地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: 不支持的方法:photos.upload 那么 photos.upload 的替代方案是什么?请给予建议,最好有一个代码示例。谢谢。

你看到我提供的答案了吗?如果它对你有用,接受一下会很好。 - PeteH
2个回答

7

1
这是一个不错的开始。但是你如何从回调中获取上传图片的URL呢?Facebook的文档中没有解释如何做到这一点。 - PeteH
回调函数会返回一个响应对象,通过响应对象,你可以获取一个GraphObject,它实际上只是一个json响应的代理。在响应中你不会得到一个url,而是会得到被创建的图片的id。 - Ming Li
@MingLi 我遇到了这个错误:Error={HttpStatus: 408, errorCode: -1, errorType: null, errorMessage: null} - Muhammad Babar

5

Ming Li提供了一些线索,但是这里有一个更完整的解决方案。这个方案已经过测试并且有效。它包含两个部分:(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();

PeteH的回复应该被标记为答案!!在postParams.putString("picture", fbPhotoAddress );中传递fbPhotoAddress,这将起到魔法般的作用。 - playmaker420

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接