cocos2d-x 把资源文件保存到本地手机上,调用JNI接口,ANdroid的

转载请注明源地址

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会有很多错误@

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值