I have a problem using cocos2d-x on android.
I am trying to load a .zip file using
CCFileUtils::sharedFileUtils()->getFileDataFromZip(...)
As this project is designed to be multiplatform, we also tried this code on an iPad3 and there it is working without problem.
The problem is that the app gets an Segmentation Fault when loading the zip file.
Here is the whole code with some extra comments
void Map::loadChunk(int index, CCPoint startPoint) {
chunks[index]->startPoint = startPoint;
std::stringstream filename;
filename << (int)startPoint.x << "_" << (int)startPoint.y << ".map";
unsigned long filesize = 0;
const char* path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("map1.zip");
CCLog(path);
CCLog(filename.str().c_str());
// In the following two lines the error occurs
const char* buffer =(const char*) CCFileUtils::sharedFileUtils()->getFileDataFromZip(path, filename.str().c_str(), &filesize);
std::istringstream fileBuffer(buffer);
CCLog("filesize %d", (int)filesize);
std::string line;
// Here some code follows but this code does not produce the problem so I left it out
}
The used includes are:
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include "cocos2d.h"
The values for path and filename are
map1.zip and -128_-128.map
map1.zip is in the assets folder and the .map file exists inside map1.zip
Thanks in advance
I've figured out why this is not working.
It seems to be the problem to pack another .zip into the .apk.
When playing the .zip in the folder received by
CCFileUtils::sharedFileUtils()->getWriteablePath()
and then open the zip with
CCFileUtils::sharedFileUtils()->getFileDataFromZip(...)
the zip loads properly and I am able to read certain files from inside the zip.
Related
I am trying to test a very basic c++ code in CppDroid app to create a text file on my Android phone (Huawei P20pro). But it doesn't seem to work. I think the problem is with the file directory. Can someone let me know how to get the real full directory of a file/folder in Android phone?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream NewFile;
NewFile.open("/Internal Storage/CppDroid/projects/project_dec23a/Gyrocopter.txt");
NewFile << "This is a file about a gyrocopter. \n";
NewFile.close();
return 0;
}
I am trying to load a file in NDK using ifstream but it fails to read it. I've double checked the path of the file and it is not wrong. The same program works in normal C++(without the JNI stuff ofcourse).
#include <jni.h>
#include <string>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>
using namespace std;
extern "C"
{
JNIEXPORT jstring JNICALL Java_com_example_aaaaatrytest_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
string file_path = "/home/moe/Desktop/blah.txt";
std::ifstream fim(file_path);
if(fim.is_open())
{
string pass = "File Loaded";
return env->NewStringUTF(pass.c_str());
}
else{
std::string fail = "Failed to load file";
return env->NewStringUTF(fail.c_str());
}
}
}
After removing if-else and debugging, this is what debugger displays:
SIGTRAP (signal SIGTRAP)
env = {JNIEnv * | 0x55bc7ccc00} 0x00000055bc7ccc00
{jobject | 0x7fcefb1af4} 0x0000007fcefb1af4
I have tried to use fstream instead of ifstream but same error. I've also provided external storage write and read permission in manifest.xml but it didn't help.
This problem is format independent as I've tried to put different files in the path. Why is it failing to read the file?
l have copied the file to my device and gave android path but it still fails to read. My android's path to file looks like this "/storage/emulated/0/abc/abc.txt".
Your app needs READ_EXTERNAL_STORAGE permission.
Here is a small snippet that helps to request this permission at runtime: see READ_EXTERNAL_STORAGE permission is in manifest but still doesn't work.
I'm writing an Adroid app with some C++ code behind the UI using Eclipse + NDK (r8d). I have some code that I thought was fool proof but the compiler just gives me weird errors like "Invalid arguments" without specifics. Here is what my C++ code looks like:
#include <jni.h>
#include <string>
using namespace std;
#include "../../Evaluator.Engine/Evaluator.Engine.h"
Evaluator evaluator;
extern "C" {
JNIEXPORT jstring JNICALL Java_haskellevaluator_android_MainActivity_evaluateNative(JNIEnv *env, jobject, jstring jInput)
{
...
string sInput(L"Hello world");
string sResult = evaluator.evaluate(sInput);
jstring jResult = env->NewStringUTF(sResult.data());
return jResult;
}
}
Evaluator.Engine.h is nothing fancy, but just a declaration of the class Evaluator.
#include <string>
using namespace std;
class Evaluator
{
public:
string evaluate(string input);
};
However, the compiler complains:
Invalid arguments '
Candidates are:
? evaluate(?)
'
as if string is not defined. But if I put a copy of the header file under the same folder, the error goes away. This is a Windows box. I have tried using \ and escaped \\ as path separators and it didn't work.
Does this sound like a NDK (or whatever the preprocessor it uses) bug? I don't want to move the header file because it'll be shared by other projects. I also hate to keep 2 copies of the same file.
Any ideas? Thanks.
Sorry I don't have windows OS, but I've tried you code on a MacOS, but it doesn't work because of:
string sInput(L"Hello world");
Saying that wchar_t cannot be put on std::string. Is it possible to be the same problem ?
I like to build the Android NDK example "native-audio", supplied with the NDK.
The NDK-supplied compiler swallows it without complaint, but Eclipse is showing several errors in file native-audio-jni.c.
In the code excerpt below, all of the static variable declarations have red underlining, because Eclipse claims that the types cannot be resolved. They are defined right up there in the OpenSLES.h file, which Eclipse does find if I Ctrl + left click the file.
Note that I modified the lines where hello_clip and android_clip are included compared to the example source - I put the array declarations in those "header" files also (they contained just comma separated data), since Eclipse had a problem parsing the syntax, too:
static const char array[] = #include"blah.h";
I tried things suggested over the internet, like starting with eclipse.exe -clean, clean project / rebuild, refresh project, etc., none helps.
So, what could be the reason/solution for Eclipse not resolving those types?
#include <assert.h>
#include <jni.h>
#include <string.h>
// for __android_log_print(ANDROID_LOG_INFO, "YourApp", "formatted message");
// #include <android/log.h>
// for native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
// for native asset manager
#include <sys/types.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
// pre-recorded sound clips, both are 8 kHz mono 16-bit signed little endian
// includes data arrays here
#include "hello_clip.h"
#include "android_clip.h"
// engine interfaces
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine;
// output mix interfaces
static SLObjectItf outputMixObject = NULL;
static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
(the file is full of many more of those problems)
I have a directory mounted by fuse,and i am intented to use lstat() against this dirctory.But,when i run the following code,it just wait there and prompt nothing.
And by the way,i run the fuse in the android emulator.
the code is:
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!");
char *path="/data/pwrite/test_12/";
struct stat *stbuf;
int res=12;
res=lstat(path, stbuf);
printf("%d",res);
}
And,"/data/pwrite/test_12/" is the fuse-mounted directory.What's more,when i try another dicrtory that share the same parent directory but not mounted by fuse,like /data/pwrite/test_13/,it works!
so,i definely sure it is leaded by fuse.But,i'm even more confused that whether it is due to the conflict between fuse and android.
Any idea?thx
Try without the bugs and see if that works better.
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!\n");
const char *path="/data/pwrite/test_13/";
struct stat stbuf;
int res=12;
res=lstat(path, &stbuf);
printf("%d\n",res);
}
why it not work?because there runs several fuse-deamon currently.