转载请注明源地址
1,创建一个类,来管理jni , NTJniHelper类
/********************************************************************
* 文件描述: jni管理者
*********************************************************************/
#ifndef _H_NTJNIHELPER_H_
#define _H_NTJNIHELPER_H_
#include "cocos2d.h"
class NTRenameDelegate {
public:
virtual void registerMyName(std::string strName) = 0;
};
class NTJniHelper {
public:
static NTJniHelper* sharedJniHelper();
static void purgeSharedJniHelper();
public: // jni相关
void copyPNG();
private:
bool init();
static NTJniHelper* m_pShare;
CC_SYNTHESIZE(NTRenameDelegate*,m_pRenameDelegate,RenameDelegate);
};
#endif // _H_NTJNIHELPER_H_
===========================
/*******************************************************************
* 文件描述: jni 管理者
*********************************************************************/
#include "NTJniHelper.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "../proj.android/jni/hellocpp/NTJni.h"
#include "platform/android/jni/JniHelper.h"
#endif
USING_NS_CC;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
extern "C" {
void Java_com_nt_common_NTJniHelper_toRegisterName(JNIEnv *env, jobject thiz, jstring strName) {
const char *pkgName = env->GetStringUTFChars(strName, NULL);
NTJniHelper::sharedJniHelper()->toRegisterName(pkgName);
env->ReleaseStringUTFChars(strName,pkgName);
}
}
#endif
NTJniHelper* NTJniHelper::m_pShare = NULL;
NTJniHelper* NTJniHelper::sharedJniHelper() {
if (m_pShare == NULL) {
m_pShare = new NTJniHelper();
if (!m_pShare || !m_pShare->init()) {
CC_SAFE_DELETE(m_pShare);
}
}
return m_pShare;
}
bool NTJniHelper::init() {
bool bRet = false;
do {
m_pRenameDelegate = NULL;
bRet = true;
} while (0);
return bRet;
}
void NTJniHelper::copyPNG()
{
CCLog("-----------Android-------------\n");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
copyPNGJni(); /* 调用的是#include "../proj.android/jni/hellocpp/NTJni.h"
*/
#endif
}
==============================================
/********************************************************************
* 文件描述: JNI.h
*********************************************************************/
#ifndef _H_NTJNI_H_
#define _H_NTJNI_H_
extern "C" {
void copyPNGJni();
}
#endif // _H_NTJNI_H_
/********************************************************************
* 文件描述: Jni app文件
*********************************************************************/
#include "NTJni.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include "../../../Classes/NTJniHelper.h"
#define CLASS_NAME "com/nt/common/NTJniHelper"
#define ClassName_VIDEO "org/cocos2dx/common/PlayVideo"
USING_NS_CC;
extern "C" {
void copyPNGJni()
{
JniMethodInfo minfo;
if(JniHelper::getStaticMethodInfo(minfo, CLASS_NAME, "copyPNG", "()V")) {
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
}
}
/*这里调用了 CLASS_NAME, 中的"copyPNG",函数*/
在copyPNG函数中写方法
public static void copyPNG(){
System.out.print("copyPngs");
String filename = "youhuijuan.png";//写res下的文件名字路径
String newFileName = "/sdcard/helloworld.png";//保存到sd卡根目录下的图片及名字
File dir = new File(newFileName);
if (dir.exists())
{
return;
}
AssetManager assetManager = ntparkure.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
===完成运行,
注意:
如果NTJni.cpp 不关闭的情况下eclipse会有很多错误@