0

How to read/write a txt file, without using Context to get Dir. Are there any "hardcoded" universal path. /data/data/com.project.name/file.mp3 ( .mp3 -protecting from compression) not working (crash with memory fault). Is it possible at all to using simply standard C or C++ streams in NativeActivity? All things gefinden in internet was about using JNI/JAVA or android_app structure (which i can't reach).

    FILE * stream = fopen("/data/data/com.mypackage.test/save.mp3", "r");
fscanf(stream, "%s", buff); //there is segfault
fclose(stream);

//more complex way -  nor crashing but opened file is empty
file.open("/data/data/com.mypackage.test/save.mp3", std::ios::in | std::ios::out);
if(!file.good())
{
    file.open("/data/data/com.mypackage.test/app_Data/save.mp3", std::ios::in | std::ios::out);
        if(!file.good())
            file.open("/data/data/com.mypackage.test/files/save.mp3", std::ios::in | std::ios::out);
}
file>>s_buff;
file.close();

The part of code which I using. Sorry about my language skills.

  • Possible duplicate of [Android - writing/saving files from native code only](http://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only) – Alex Cohn Oct 17 '15 at 08:32
  • I saw this before. He is using void android_main(struct android_app* state), i must do it without this. – JustRandomMe Oct 17 '15 at 08:37
  • Please check this [answer](http://stackoverflow.com/a/6284443/192373). The bottom line is, yes, you can use simple C++ streams, and you don't need to hardcode the package name for that. But still you must check that the file is present. Your *memory fault* part looks suspicious. Maybe you should post the piece of code and/or stack trace for the crash. – Alex Cohn Oct 17 '15 at 08:53
  • tag - libc (both) error: fatal signal 11(SIGSEGV) at 0x00000004 (code =1), thread 24995 Send stop signal to pid:24977 in void debugger_signal_handler(int siginfo_t*, void*) ~error masage from logcat ADT Eclipse Thanks for your answer, but i don't know how to read "/proc/MyPID/cmdline". I never working with linux kernel or android before. – JustRandomMe Oct 17 '15 at 10:02
  • You can check the pointer returned from fopen(), don't need complex ways. BTW why do you think the file should be there? – Alex Cohn Oct 17 '15 at 12:25
  • I put file into asset folder... and all other folders in project. It's just my config files. Sorry if that's were not clearly at start. – JustRandomMe Oct 17 '15 at 13:22
  • Assets are not unpacked when the app is installed. You must do it yourself in Java. – Alex Cohn Oct 17 '15 at 13:23
  • .mp3 or .png formats are packaged too? I use they to avoid zip compresion. It's can be hard read in java using NativeActivity, but if there any other options, i will try. It must be possible :). Thank you very much. – JustRandomMe Oct 17 '15 at 13:37
  • Packing and unpacking of assets is a whole different story. But never they are converted to files by installer. When you request zipped assets via asset API - Java or native - they are unzipped for you into individual files. For .mp3 and other non-zip panel assets, you receive the file handle to your APK with offset that points to that specific asset. This way, read-only access to these resources is very efficient, but only possible through asset API. – Alex Cohn Oct 17 '15 at 17:12
  • How about writing into file? It is possible to unpack(zlib), overwrite and pack again? In other app, gzfile after open is always false. Can i write into .apk? – JustRandomMe Oct 19 '15 at 09:50
  • No, you cannot write to APK file – Alex Cohn Oct 19 '15 at 18:50
  • Possible duplicate of [How to set file path in Android JNI?](http://stackoverflow.com/questions/22799041/how-to-set-file-path-in-android-jni) – Alex Cohn Oct 22 '15 at 11:32

0 Answers0