【问题标题】:Android Development: Post Screencap Image to Facebook wallAndroid 开发:将 Screencap Image 发布到 Facebook 墙上
【发布时间】:2013-06-03 04:09:53
【问题描述】:

我一直在寻找使用 android 和 facebook sdk 将图像 + 文本发布到 facebook 的正确方法。我可以让这段代码只发布一条消息,但是当我尝试发布屏幕截图时,我所做的最终是一个空指针异常。我从其他线程尝试了许多不同的代码 sn-ps,但似乎没有一个对我有用。我能做的最好的就是这段代码,它会在我的墙上发布一条消息,而不是照片。请告诉我我在这里做错了什么。非常感谢。

View content = findViewById(R.id.row1);
        Log.d("captureScreen", content.getId()+"");
        byte[] byteArray=null;
        Bitmap screenshot=null;
        try {
            if (content != null) {
                int width = content.getWidth();
                int height = content.getHeight();

                screenshot = Bitmap.createBitmap(width, height,
                        Bitmap.Config.RGB_565);
                content.draw(new Canvas(screenshot));
                Log.d("captureScreen", "success");
            }
        } catch (Exception e) {
            Log.d("captureScreen", "Failed");
        }

        try{


            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();
            Log.d("byte array", "sucesss");
            } catch(Exception e){
                Log.d("byte array", "failed");
            }




        Request.Callback callback = new Request.Callback() {

            public void onCompleted(Response response) {


                JSONObject graphResponse = response.getGraphObject()
                .getInnerJSONObject();
                Log.d("onCompleted", "2 sucess");
                String postId = null;
                try {
                    postId = graphResponse.getString("id");
                    Log.d("onCompleted", "3 sucess");
                } catch (JSONException e) {
                    Log.d("json failed", "failed");
                    Log.i(TAG, "JSON error " + e.getMessage());
                }

                FacebookRequestError error = response.getError();
                if (error != null) {

                } else {

                    CreateDialog("your post was a sucess", "posted");
                }
            }

        };



      Bundle params = new Bundle();
      params.putString("message","tester"); 
    //params.putString("method", "photos.upload");
    //params.putByteArray("source", byteArray); 
    // params.putString("caption", "test Caption");

        Request request = new Request(session, "me/photos", params,
                HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();

【问题讨论】:

    标签: android eclipse facebook photo


    【解决方案1】:

    在我的应用程序中,我使用以下代码在 Facebook 中分享照片 + 文字。

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yourbitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    data = baos.toByteArray();
    Bundle params = new Bundle();
    params.putString(Facebook.TOKEN, facebook.getAccessToken());
    params.putString("method", "photos.upload");
    params.putByteArray("picture", data);
    params.putString("caption",
        "This is screen shot from MyApp. Visit my application blah blah blah");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST",
        new SampleUploadListener(), null);
    

    【讨论】:

    • 您好,非常感谢您的回复,当我将这段代码放入其中说 SampleUploadListener 无法解析为某个类型时,这是否意味着我在某处搞砸了我的 facebook 类?再次感谢
    • @itburnz,你能给我链接你正在使用哪个 FB API 吗? SampleUploadListener 只不过是有这个实现“公共类 SampleUploadListener 扩展 BaseKeyListener 实现 RequestListener”
    • 嗨,我从这里的下载链接获得了 sdk developers.facebook.com/docs/getting-started/…
    • 没有类名为“BaseKeyListener”吗?和名为“RequestListener”的接口?你开玩笑说需要通过实现这两个来创建一个类,你就完成了......让我检查你的开发人员的链接。我使用的是 API v2,而您使用的是 API v3。
    • 不,没有名为 basekeylistner 或 requestlistener 的类,嗯,我应该降级到 v2 吗?我听说有一些差异,我看到人们使用的很多东西我们都弃用了。
    猜你喜欢
    • 2011-07-07
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多