快速新增簡單的Android截圖分享
基於cocos2d-x開發(我的是V2.2.3),分享的方法有很多,例如用SharedSDK,或者直接用各個開發平臺的SDK,但對於android平臺,如果要求不是很高的話,有一種簡單的方式(Intent元件+jni)。
1.假如工程名是LCX,那首先修改LCX.java
public static void nativeShare()
{
//需要開執行緒執行
new Thread(new Runnable()
{
@Override
public void run() {
Intent shareIt = new Intent("android.intent.action.SEND");
shareIt.setType("image/*");
shareIt.putExtra(Intent.EXTRA_SUBJECT, "Share");
shareIt.putExtra(Intent.EXTRA_TEXT, "Content");
//分享sd卡的圖片shareIt.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:////data/data/" + getContext().getApplicationInfo().packageName+ "/files/myShare.png"));
shareIt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(Intent.createChooser(shareIt, "Share"));
}
}).start();
}
2.然後是cocos2d-x截圖分享部分
包含標頭檔案:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#endif
然後在分享回撥中加入程式碼:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCRenderTexture* pTexture = CCRenderTexture::create((int)winSize.width, (int)winSize.height,kCCTexture2DPixelFormat_RGBA8888);
pTexture->getSprite()->setAnchorPoint( ccp(0.5f,0.5f) );
pTexture->setPosition( ccp(winSize.width/2, winSize.height/2) );
pTexture->setAnchorPoint( ccp(0.5f,0.5f) );
pTexture->begin();
this->visit();
pTexture->end();//儲存檔案,路徑在data/data/包名/files/中
pTexture->saveToFile("myShare.png",kCCImageFormatPNG);
//分享
JniMethodInfo jni_method_info;
if (JniHelper::getStaticMethodInfo(jni_method_info, "com/test/LCX", "nativeShare", "()V")) {
jni_method_info.env->CallStaticVoidMethod(jni_method_info.classID, jni_method_info.methodID);
}#endif //CC_PLATFORM_ANDROID
這樣基本就ok了,系統會搜尋所有可以分享的應用(新浪微博,騰訊微博,人人等),然後列出一個選單供使用者選擇。
3.另外,CCRenderTexture的saveToFile方法儲存的檔案不是所有應用都可以訪問的,例如新浪微博,所以建議把檔案儲存在sdcard中,這樣就方便很多應用訪問了。
android的sdcard路徑是不定的,各個手機不一定相同,不能寫死。但是貌似cocos2dx沒有介面返回sd的路徑,所以我們自己通過jni獲取。
首先新增sd寫許可權,修改AndroidManifest.xml,如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然後修改LCX.java
public static String nativeGetSDPath()
{
//判斷sd卡是否存在
if (Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED))
{
return Environment.getExternalStorageDirectory().toString();//獲取跟目錄
}
return null;
}然後替換saveToFile的程式碼(紅色部分)
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCRenderTexture* pTexture = CCRenderTexture::create((int)winSize.width, (int)winSize.height,kCCTexture2DPixelFormat_RGBA8888);
pTexture->getSprite()->setAnchorPoint( ccp(0.5f,0.5f) );
pTexture->setPosition( ccp(winSize.width/2, winSize.height/2) );
pTexture->setAnchorPoint( ccp(0.5f,0.5f) );
pTexture->begin();
this->visit();
pTexture->end();
pTexture->saveToFile("myShare.png",kCCImageFormatPNG);
JniMethodInfo t;
std::string ret("");
if (JniHelper::getStaticMethodInfo(t, "com/test/LCX", "nativeGetSDPath", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
ret = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);CCImage *pImage = pTexture->newCCImage(true);
if (pImage)
{
std::string fullpath = ret + "/myShare.png";
pImage->saveToFile(fullpath.c_str(), true);
}
CC_SAFE_DELETE(pImage);
}
JniMethodInfo jni_method_info;
if (JniHelper::getStaticMethodInfo(jni_method_info, "com/test/LCX", "nativeShare", "()V")) {
jni_method_info.env->CallStaticVoidMethod(jni_method_info.classID, jni_method_info.methodID);
}最後修改nativeShare函式 shareIt.putExtra(Intent.EXTRA_STREAM,Uri.parse(nativeGetSDPath()+"/myShare.png"));
shareIt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getContext().startActivity(Intent.createChooser(shareIt, "Share"));好了大功告成!!!
後續:
cocos2d-x 3.2後有新的介面,utils::captureScreen,具體參考http://cn.cocos2d-x.org/tutorial/show?id=1737