cocos2d-x添加分享

来源:互联网 发布:淘宝客服链接地址 编辑:程序博客网 时间:2024/05/16 04:48

cocos2d-x添加分享 

2013-10-02 21:18:23|  分类:cocos2d-x |  标签:android cocos2d-x  分享  |字号订阅

1》安卓部分
纯安卓平台添加分享代码很简单,见红色部分。但是cocos2d-x截屏之后默认将图片保存在data/data/包名/files下,如果用来分享,其它程序是无法访问该私有目录的,所以需要将生成在该目录下的分享图片拷贝至sd卡中。

public static void share(String msg){ try {File share = getContext().getFilesDir().listFiles()[0];InputStream is = new FileInputStream(share);String path = Environment.getExternalStorageDirectory().getPath() + "/result.jpg";FileOutputStream os = new FileOutputStream(path);byte[] buffer = new byte[1024];int count = 0;while ((count = is.read(buffer)) > 0) {os.write(buffer, 0, count);}is.close();os.close();Intent intent = new Intent("android.intent.action.SEND");intent.setType("image/*");intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path)));intent.putExtra(Intent.EXTRA_SUBJECT, "捏气泡");intent.putExtra(Intent.EXTRA_TEXT, msg);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);classname.getContext().startActivity(Intent.createChooser(intent, "分享"));} catch (Exception e) {}}


2》cocos2d-x部分

void ResultScene::menuShareCallback(CCObject* pSender){CCSize size = CCDirector::sharedDirector()->getWinSize(); //定义一个屏幕大小的渲染纹理 CCRenderTexture* pScreen = CCRenderTexture::create(size.width,size.height, kCCTexture2DPixelFormat_RGBA8888); //获得当前的场景指针 CCScene* pCurScene = CCDirector::sharedDirector()->getRunningScene(); //渲染纹理开始捕捉 pScreen->begin(); //当前场景参与绘制 pCurScene->visit(); //结束捕捉 pScreen->end();//保存为png//pScreen->saveToFile("result.png", kCCImageFormatPNG); //保存为jpgpScreen->saveToFile("result.jpg", kCCImageFormatJPEG); CC_SAFE_DELETE(pScreen); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台char shareMsg[100]; sprintf(shareMsg,"嗨!快来挑战吧O(∩_∩)O~",g_result);const char *message = UTF8CHS(shareMsg).c_str();//需要传递到Java层的字符串JniMethodInfo methodInfo;bool isHave = JniHelper::getStaticMethodInfo(methodInfo,"package/class",//需要调用的Java文件"function",//调用的方法名"(Ljava/lang/String;)V");//参数if(isHave){ jstring StringArg = methodInfo.env->NewStringUTF(message);methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID,StringArg);}else{CCLog("the showMessage method is not exits");} #endif}


后注:
原来打算把预先准备的图片放在drwa或raw目录,然后分享,发现参照网络上的方法,比如:
android如何获得drawable或raw或assert目录下的图片的uri地址  但是木有成功。
参考:
Intent分享图片
android cocos2dx游戏-添加截屏并分享微博功能   这个方法我也试了木有成功
0 0