在做游戏界面截图经常使用,如果想把截图图片保存到SD卡就需要这样做了。
截图的图片保存在,游戏包Files文件夹下面。
别忘了把android写入文件和读取文件的权限开了。
代码
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();
int id = spritewith->getTag() ;
char temp[20];
sprintf(temp,"gril%d.jpg",id);
pScreen->saveToFile(temp, kCCImageFormatJPEG);
CC_SAFE_DELETE(pScreen);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
JniMethodInfo methodInfo;
bool isHave = JniHelper::getStaticMethodInfo(methodInfo,
"cn/partygame/sexyair/SexyAir",//需要调用的Java文件
"share",//调用的方法名
"(I)V");//参数
if(isHave){
jint ji = id ;
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID,id);
//CCLog("ok");
}else{
//CCLog("the showMessage method is not exits");
}
#endif
需要的头文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform\android\jni\JniHelper.h"
#endif
在eclipse,包名.java文件下面添加如下代码:
java代码
public static void share(final int id){
try {
StringBuilder builder = new StringBuilder();
builder.append("gril").append(id).append(".jpg");
String s = builder.toString();
File[] files = getContext().getFilesDir().listFiles();
File share = null;
for (int i = 0; i < files.length; i++)
{
File file = files[i];
if (file.getName().endsWith(s)) {
share = file;
break;
}
}
if (share == null)
return;
InputStream is = new FileInputStream(share);
//String path = Environment.getExternalStorageDirectory().getPath() +s;
String localPath=Environment.getExternalStorageDirectory().getAbsolutePath() + "/image";
File f=new File(localPath);
if(!f.exists())
{
f.mkdir();
}
String path = localPath + "/" + s;
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();
} catch (Exception e) {
}
}
}
这样就点击按钮就可以响应截图以及保存到本地文件夹下面。