Android用户生成的Open Graph图像

时间:2013-11-13 15:27:33

标签: android facebook facebook-graph-api

我一直在按照教程创建一个开放的图形故事here。一切都很好,但我希望上传的图像很大,即“用户生成”。

我的问题在于实际实现这一点。这是从SD卡获取图像的代码:

// If uploading an image, set up the first batch request
 // to do this.
     // Set up image upload request parameters
     Bundle imageParams = new Bundle();
     Bitmap image = BitmapFactory.decodeFile(getActivity().getIntent().getStringExtra("image_for_facebook"));

     // Set up the image upload request callback
     Request.Callback imageCallback = new Request.Callback() {

         @Override
         public void onCompleted(Response response) {
             // Log any response error
             FacebookRequestError error = response.getError();
             if (error != null) {
                 dismissProgressDialog();
                 Log.i(TAG, error.getErrorMessage());
             }
         }
     };
     // Create the request for the image upload
     Request imageRequest = Request.newUploadStagingResourceWithImageRequest(Session.getActiveSession(),
     image, imageCallback);

     // Set the batch name so you can refer to the result
     // in the follow-on object creation request
     imageRequest.setBatchEntryName("imageUpload");


     // Add the request to the batch
     requestBatch.add(imageRequest);

以下是随后给图形对象赋予其属性的位:

 // Set up the OpenGraphObject representing the book.
    OpenGraphObject book = OpenGraphObject.Factory.createForPost("books.book");
    // Set up the book image; if we uploaded the image, it is the "uri" result from the previous
    // batch request, otherwise it is just a URL.
    String imageUrl = "{result=imageUpload:$.uri}";

    book.setImageUrls(Arrays.asList(imageUrl));

    book.setTitle("A Game of Thrones");
    book.setUrl("https://facemash.biz/books/a_game_of_thrones/");
    book.setDescription("In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.");
    // books.book-specific properties go under "data"
    book.getData().setProperty("isbn", "0-553-57340-3");

如何将here显示的图像的“user_generated”参数设置为true,我该怎么做? (当然假设我认为这是我需要做的就是获得大图像而不是缩略图)。

谢谢!

2 个答案:

答案 0 :(得分:2)

查看SelectionFragment.java中的Scrumptious示例,即getImageObject方法。基本上,您需要使用“url”和“user_generated”属性创建图形对象:

    GraphObject imageObject = GraphObject.Factory.create();
    imageObject.setProperty("url", "{result=imageUpload:$.uri}");
    imageObject.setProperty("user_generated", "true");
    GraphObjectList<GraphObject> images = GraphObject.Factory.createList(GraphObject.class);
    images.add(imageObject);
    book.setImage(images);

答案 1 :(得分:0)

明的回答对我来说非常接近。为了将图像附加到OpenGraphAction,我必须执行以下操作:

GraphObject imageObject = GraphObject.Factory.create();
imageObject.setProperty("url", imageUrl);
imageObject.setProperty("user_generated", true);
List<JSONObject> images = new ArrayList<JSONObject>();
images.add(imageObject.getInnerJSONObject());
action.setImage(images);

Open Graph Actions的方法为setImage(List<JSONObject> images),但我找不到setImage(GraphObjectList<GraphObject>)方法。我正在使用Facebook SDK v3.6,因此可能已经改变。