在我的跨平台cocos2dx游戏中,我想从Web上异步加载一些图像并将它们保存到本地存储。对于Android部分,我使用JNI调用下载并使用本机java代码将映像保存到存储。它完美无缺。但是当我尝试在一个单独的pthread中执行此操作以防止我的游戏冻结时,我得到错误:"本机线程退出而没有分离"在" ThreadFunction(void * arg)"完了。
这是我的c ++代码:
//c++ code
void MyClass::loadImages(){
pthread_t thread;
SimpleStructure* args = new SimpleStructure();
args->s_url = url;
args->s_savename = savename;
pthread_create(&thread, NULL, &ThreadFunction, args);
}
void* ThreadFunction(void * arg) {
SimpleStructure * args = (SimpleStructure * ) arg;
std::string localImagePath;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
localImagePath = getImageFilePathJNI(args->s_url.c_str(),
args->s_savename.c_str());
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
localImagePath = URLSpriteHelper::getUrlSprite(args->s_url,
args->s_savename);
#endif
pthread_mutex_lock(&mutex);
loadedImages.push_back(localImagePath);
pthread_mutex_unlock(&mutex);
return NULL;
}
这是我的JNI代码:
extern "C"
{
std::string getImageFilePathJNI(const char* urlStr, const char* saveName)
{
std::string ret;
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t,
"org/cocos2dx/cpp/AppActivity",
"saveImageFromURLtoInternalStorage",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")) {
jstring StringArg1 = t.env->NewStringUTF(urlStr);
jstring StringArg2 = t.env->NewStringUTF(saveName);
jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, StringArg1, StringArg2);
const char* str = t.env->GetStringUTFChars(retFromJava, 0);
ret = str;
t.env->ReleaseStringUTFChars(retFromJava, str);
t.env->DeleteLocalRef(StringArg1);
t.env->DeleteLocalRef(StringArg2);
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
}
任何想法我做错了什么?
非常感谢!
答案 0 :(得分:0)