[Android] Share screenshot game from local resource to facebook wall

I saved my screenshot game with it :
www.cocos2d-x.org/wiki/How_to_Save_a_Screenshot

I shared to facebook wall with it :

if (FacebookDialog.canPresentShareDialog(getApplicationContext(),FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) { Log.i("Shareee", "Shareee"); // Publish the post using the Share Dialog FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this) .setName("Reon Neon Game") .setCaption("Welcome 2 Hell") .setDescription("Fire in the hole http://i.imgur.com/1yN9PDV.png") .setLink("http://www.facebook.com/") .setPicture("http://i.imgur.com/1yN9PDV.png") .build(); uiHelper.trackPendingDialogCall(shareDialog.present());

I have a question , how a can replace this
".setPicture("http://i.imgur.com/1yN9PDV.png")"
to share my screenshot to facebook wall

Thanks for reading :slight_smile:

any help? :smiley:

:frowning:

Did you mean how to catch screenshot?

@zhangxm Uhm, i thinks, he wants to know how to send a screenshot to his facebook wall, (like capture your game’s scores and post it on facebook).

I think it is possible to read GL frame buffer data when a scene is drawn. You can use Director::setNotificationNode() to register a callback when a scene is drawn. And read GL frame buffer data the NotificationNode::visit().

@zhangxm wrote:

I think it is possible to read GL frame buffer data when a scene is drawn. You can use Director::setNotificationNode() to register a callback when a scene is drawn. And read GL frame buffer data the NotificationNode::visit().

No, I saved my screenshot but i don’t know how to send my screenshot to Facebook wall

Oh, i misunderstood it.
I am sorry. I am not familiar with it.

@tieunun i think you only can upload photo from screenshot game from local resource to facebook wall .
if you want to share photo , you only use link of picture .

Thanks xuyenit55 & zhangxm so much.
It’s bad to hear i can’t do that :frowning:

@tieunun
you can upload the image to facebook first, and you will get the id of the image, you can get the image’s url through the id.

@zhangxm
hi, 我在Android平台使用cocos2d-x v2.2.1的截图功能时, 截取到的图片保存到存储卡后为全黑色,代码如下 :

    CCSize size = CCDirector::sharedDirector()->getWinSize();

    CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height, kCCTexture2DPixelFormat_RGBA8888);    
    texture->setPosition(ccp(size.width/2, size.height/2));    
    texture->begin();
    CCDirector::sharedDirector()->getRunningScene()->visit();
    texture->end();

     //保存为png  
    bool result = texture->saveToFile("scrshot.png", kCCImageFormatPNG); 

请问如何解决 ? 谢谢

@mrsimple wrote:

@tieunun
you can upload the image to facebook first, and you will get the id of the image, you can get the image’s url through the id.

Hi mrsimple, can you share me demo source code or the method get id of the image and the method get the image’s url through the id.
Thank :slight_smile:

@tieunun hope it can hope you :smiley:

void GameLayer::menuFBShareCallback(CCObject* pSender)
{
	cocos2d::plugin::TShareInfo pInfo;
	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
	CCRenderTexture *screenshot = CCRenderTexture::create(int(winsize.width), int(winsize.height), kCCTexture2DPixelFormat_RGBA8888);
	screenshot->setPosition(ccp(winsize.width / 2, winsize.height / 2));
	screenshot->begin();
	CCDirector::sharedDirector()->getRunningScene()->visit();
	screenshot->end();
	screenshot->saveToFile("screenshot.jpg", kCCImageFormatJPEG);
	std::string dir =  CCFileUtils::sharedFileUtils()->getWritablePath();
	dir.append("screenshot.jpg");
	pInfo["LinkShare"] = "http://gamesv.vn/product/pikachu-hd-2014-7.html#&slider1=2";
	// call plugin facebook 
	FacebookManager *mFace ;
	mFace = FacebookManager::sharedSocialManager();
	mFace->loadSocialPlugin();
	mFace->shareByMode(pInfo);
}
// upload photo facebook 
// function of android 
static class Getbitmap extends AsyncTask<Void, Void, Void> {
		//Bitmap bitmap;
		Bitmap bitmap;
		@Override
		protected Void doInBackground(Void... paramds) {
			try {
			// shareFile is path of the image file from resource
				bitmap = BitmapFactory.decodeFile(shareFile);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			shareContent = SocialFacebook.DESCRIPTION_SHARE;
			Photo photo = new Photo(bitmap);
			photo.addDescription(shareContent);
			// publish
			gFacebook.publish(photo, new OnPublishListener() {
				ProgressDialog dialog;

				@Override
				public void onFail(String reason) {
					// insure that you are logged in before publishing
					if (dialog != null)
						dialog.dismiss();
					Log.w(TAG, "Failed to publish" + reason);
				}

				@Override
				public void onException(Throwable throwable) {
					if (dialog != null)
						dialog.dismiss();
					Log.e(TAG, "Bad thing happened", throwable);
				}

				@Override
				public void onThinking() {
					dialog = new ProgressDialog(mContext);
					dialog.setCancelable(false);
					dialog.setMessage("Processing...");
					dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					dialog.setMax(100);
					dialog.show();
					// show progress bar or something to the user while
					// publishing
				}
				@Override
				public void onComplete(String id) {
					dialog.dismiss();
				}
			});
			super.onPostExecute(result);
		}
	}
	// call function share
	public static void sharefb() {
			 new Getbitmap().execute();
		}

@tieunun

1、 upload the image to facebook first

     private void uploadImage(Bitmap mUploadBitmap) {
        // 上传图片
        if (mUploadBitmap != null && !mUploadBitmap.isRecycled()) {
            // // 上传图片的请求, 可以用
            Request request =
                    Request.newUploadPhotoRequest(Session.getActiveSession(), mUploadBitmap,
                            mUploadCallback);
            request.executeAndWait();
        }
    }


    /**
     *  image uploaded, you can get the image id.
     */
    private Callback mUploadCallback = new Callback() {

        /**
         * (非 Javadoc)
         * 
         * @Title: onCompleted
         * @Description:
         * @param response
         * @see com.facebook.Request.Callback#onCompleted(com.facebook.Response)
         */
        @Override
        public void onCompleted(Response response) {
            if (response.getError() != null) {
                Log.d(TAG, response.getError().toString());
            } else {
                // 图片上传成功,获取response 的 json对象
                JSONObject jsonObject = response.getGraphObject()
                        .getInnerJSONObject();
                try {
                    // get image id
                    String imgId = jsonObject.getString("id");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } // end of onCompleted
    };

2、 you can get the image’s url through the id

/**
     * </br>
     */
    private String doImageUrlRequest(String imageID) {
        String FACEBOOK_URL = "https://graph.facebook.com/?";
        // 构造http请求来获取图片地址
        HttpGet httpget = new HttpGet(FACEBOOK_URL + "id=" + imageID
                + "&access_token=" + Session.getActiveSession().getAccessToken());

        HttpParams httpparams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpparams, 15000);
        HttpConnectionParams.setSoTimeout(httpparams, 30000);

        String imageUrl = "";
        // http client
        HttpClient httpclient = new DefaultHttpClient(httpparams);
        try {
            HttpResponse response = httpclient.execute(httpget);

            if (response.getStatusLine().getStatusCode() == 200) {
                // 获取json data
                String json = streamToJson(response.getEntity().getContent());

                // get the image url
                if (json.contains("source")) {
                    imageUrl = getImageUrl(json);
                } else {
                    Log.e(TAG, "### 获取分享到facebook的图片url失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return imageUrl;
    }
1 Like

@xuyenit55
hi, i use this code to take screenshot on Android, but i get the black image, is it something wrong ? Thanks. ( Cococs2d-x V2.2.1 )

  CCSize winsize = CCDirector::sharedDirector()->getWinSize();
    CCRenderTexture *screenshot = CCRenderTexture::create(int(winsize.width), int(winsize.height), kCCTexture2DPixelFormat_RGBA8888);
    screenshot->setPosition(ccp(winsize.width / 2, winsize.height / 2));
    screenshot->begin();
    CCDirector::sharedDirector()->getRunningScene()->visit();
    screenshot->end();
    screenshot->saveToFile("screenshot.jpg", kCCImageFormatJPEG);

@mrsimple i use it in cocos2dx 2.2.2 , Can you post code which post image in facebook ?

Thanks all u, i will try it
With taking screenshot, the source code from here : http://www.cocos2d-x.org/wiki/How_to_Save_a_Screenshot and iam using 2.2.2, too :smiley:

Hi !
I’m having problems share facebook.
I follow the instructions to run cocos2d-x share facebook
path: D: \ SetupGame \ cocos2d-x-3.4 \ plugins \ samples \ HelloPlugins.
I can not run the demo.
help me. Thanks

error start eclipse
Unable to get provider com.facebook.NativeAppCallContentProvider: java.lang.ClassNotFoundException: Didn’t find class “com.facebook.NativeAppCallContentProvider” on path: /data/app/org.cocos2dx.HelloPlugins-2.apk