【问题标题】:Android Facebook SDK 3.0 upload local imageAndroid Facebook SDK 3.0 上传本地图片
【发布时间】:2013-06-02 15:56:39
【问题描述】:

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

我关注了this tutorial on FB site,它给出了一个从 Web URL 发布图片的示例: postParams.putString("图片", "https:// 图片 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:不支持的方法,photos.upload

那么,photos.upload 替代品是什么?请告知,一个代码示例会很棒,tnx。

【问题讨论】:

  • 你看到我提供的答案了吗?如果它对你有用,你会接受它会很好。

标签: android facebook image


【解决方案1】:

如果你想上传照片,为什么不直接使用 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();

【讨论】:

  • 这是一个好的开始。但是如何从回调中获取上传图片的 URL 呢? Facebook 文档没有说明如何做到这一点。
  • 回调给你一个 Response 对象,从 Response 对象中你可以得到一个 GraphObject,它实际上只是一个 json 响应的代理。你没有在响应中得到一个 url,你得到的是创建的图像的 id。
  • @MingLi 我收到了这个Error={HttpStatus: 408, errorCode: -1, errorType: null, errorMessage: null}
【解决方案2】:

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;这会变魔术。
猜你喜欢
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 2013-03-05
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多