-2

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

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();
4

1 回答 1

0

在我的应用程序中,我使用以下代码在 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);
于 2013-06-03T07:23:32.393 回答