Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: android - i/o file  (Read 5137 times)

0 Members and 1 Guest are viewing this topic.

Kori

  • Newbie
  • *
  • Posts: 21
    • View Profile
android - i/o file
« on: October 15, 2015, 08:56:20 pm »
I know, this is not android forum, but after three days and nights i want to try everything.

My sfml/android test game is almost done. Last thing is game save state. But i have totally no idea how to implement it. I already tried C stdio.h and c++ streams, getting path from ANativeActivity::externalDataPath
and much more. Is it possible at all to read/write a .txt file using only ndk? Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: android - i/o file
« Reply #1 on: October 15, 2015, 09:06:28 pm »
What's the problem? Could you get a detailed error code or message that would help to figure out what's wrong?

What does ANativeActivity::externalDataPath contain? If it refers to the SD card, make sure you have one inserted ;)

And which version of Android does your device use?
Laurent Gomila - SFML developer

Kori

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: android - i/o file
« Reply #2 on: October 15, 2015, 09:53:14 pm »
Testing on 2.3.7 and 4.4.2.
I try both, with externalDataPath  and externalDataPath.
To combine the default path with my file name i write something like that:
strcpy(name,asset->internalDataPath);
strcat(name, "/cnf.png");

I also trying just "/data/data/com.example.sfml/cnf.png", "/sdcard/cnf.png" and other similar path.

.png extension is to prevent file from being compressed. Also tested with .mp3, .txt, and without any extension.

Next, I was trying to read using ">>" from c++ or fscanf(stream, "%s", var"); from C
Always result is same,  crash with lovely SIGSEGV when reading starts. My "cnf" file is in assets folder and in main project folder. Now, I have't any concept how write/read function should looks like.

I hope you understand me,really sorry if something sounds unfriendly/unkindly, I still don't operate English very well.


DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
I would like a spanish/latin community...
Problems building for Android? Look here

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: android - i/o file
« Reply #4 on: October 16, 2015, 08:38:39 am »
Have you had a closer look at logcat? It should tell you more, not just some segfault.

Kori

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: android - i/o file
« Reply #5 on: October 16, 2015, 12:03:17 pm »
It's a standard segfault, because i am trying to open non-existing file.

http://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
Yes, that's the answer, but i don't know how to implement it. Changing default int main with  void android_main(struct android_app* state) and moving this into NatwiveActivity.cpp throw
 "Description   Resource   Path   Location   Type
make.exe: *** [obj/local/armeabi-v7a/libsfml-example.so] Error 1   NativeActivity          C/C++ Problem"

If my logic is proper, android_app must be taken from main function, and is required to get file path.

So, easy fopen() won't be working? I must do it using NDK API? It's much harder than making game on PC....

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: android - i/o file
« Reply #6 on: October 16, 2015, 12:31:05 pm »
Correct, it's not exposed yet right now, so you'd have to modify SFML's source to expose the native activity. A patch is in the works (I have it here, just lacks commit and some proper documentation). You should be able to use usual file streams, but you'll have to set the proper permission in your AndroidManifest.xml file.
« Last Edit: October 16, 2015, 12:34:41 pm by Mario »

Kori

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: android - i/o file
« Reply #7 on: October 16, 2015, 01:50:48 pm »
   
I already had something like that:
     
(click to show/hide)

Modifying code is out of range of my newbie skills, especially in NDK. Last thing which I do, will be rewrite code on other system. My Windows is pretty freakish.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: android - i/o file
« Reply #8 on: October 16, 2015, 02:02:23 pm »
No worries, a branch (and pull request) will be up soon™. :)

Kori

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: android - i/o file
« Reply #9 on: October 16, 2015, 10:16:36 pm »
I can't wait, you are doing awesome job (whole SFML team) :).

short (hope last) question:
Quote
You should be able to use usual file streams,
Now or using possibility of access to NativeActivity in future? If now, i just can't solve the right path.
Thank you for your help.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: android - i/o file
« Reply #10 on: October 17, 2015, 03:46:49 pm »
Now, but I haven't really experimented with that. It might require you to figure out the actual paths (which might be hard without the native activity).

Kori

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: android - i/o file
« Reply #11 on: October 17, 2015, 04:16:41 pm »
No, hardcoded path is imo bad idea. After sleep on in, i can't figure all possibly path on all android devices. I just sit down and wait for commit or try to read value from /proc/pid/cmdline. Better be patient than loosing another week on google research.

@BIG EDIT
Sorry,your pain in neck back for a moment. I promise, it's last time in this month (probably).

IT'S ALIVE. It's looking awful but works. SFML code is more readable than I guess. Brutally added NativeActivity pointer solve the problem. But...
Now i can't run it on IOS, right? I need last simply/short answer, is it will be hard to recompile it for Iphone ?
« Last Edit: October 17, 2015, 06:15:52 pm by Kori »