5

这似乎没有明确的参考。我正在创建一个用户可以登录到 FB 的 Android 应用程序。

我在 FB 网站上遵循了本教程,其中给出了从 Web URL 发布图片的示例: postParams.putString("picture", "https:// image URL");

但是,我想将我的项目中的本地 PNG 图像上传到登录用户的时间轴,该图像位于所有 res-drawable 文件夹中。

这是我的代码:

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 替代品是什么?请告知,一个代码示例会很棒,tnx。

4

2 回答 2

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();
于 2013-06-05T16:57:35.237 回答
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();
于 2013-07-11T05:35:53.227 回答