How to load a binary file from the resources lazarus / 43-Android ---------------------------------------------------------------------- Kostas · 2012-06-10 09:02 Hello all! I have managed to compile the androidlcltest project and run it in my Galaxy Note Smartphone without big problems. Now i wanted to go a step further and compile my neural network based backgammon engine with lazarus. So i need to load the file with the paramters of the neural network. I have put the file in the raw directory and it seems to be added to the apk android file at the end. How can i now load the file? To open a file stream does not seem to work, because the parameters file is embedded in the apk file as a resource. So how can i access these resource data? Thanks in advance Kostas ---------------------------------------------------------------------- felipemdc · 2012-06-10 12:29 When the application is installed the APK is uncompressed and the files are stored in the file system. From what I remember resource files will go to a directory /data/data/ maybe /res/ afterwards too. So for the example: /data/data/com.pascal.lcltest/ Using "adb shell" you can access the directory and see for yourself where things are. Your program always has read/write access to your own directory and you can use TFileStream or anything else to access files there. Later I will check this better, but I thought I'd give a partial answer anyway now as it will probably get you going. ---------------------------------------------------------------------- Kostas · 2012-06-10 15:06 Thanks a lot. I will try to give it a try as you describe it. I have i have something new i will let you know. Regards Kostas ---------------------------------------------------------------------- meanderix · 2012-06-14 13:49 Quote from: Kostas on June 10, 2012, 11:02:07 amI have put the file in the raw directory and it seems to be added to the apk android file at the end. How can i now load the file? To open a file stream does not seem to work, because the parameters file is embedded in the apk file as a resource. So how can i access these resource data? Ah, I didn't know about this approach. A different option would be to include the data in a resource script (.rc file.) That should work too. Mattias ---------------------------------------------------------------------- meanderix · 2012-06-24 13:28 I looked at this further and I don't think the raw files are extracted to /data/data/. I think you need to use getResources().openRawResource(R.raw.yourfile) to access the resource file (from the Java side.) However, I would recommend using a resource script since then you can access it directly in pascal. ---------------------------------------------------------------------- Kostas · 2012-06-25 17:12 Quote from: meanderix on June 14, 2012, 03:49:42 pmAh, I didn't know about this approach. A different option would be to include the data in a resource script (.rc file.) That should work too. This does not seem to work when compiling for Android. I created a .rc file and the data are included to the executable when i compile for Win32. But when i compile for Android i get an error: androidlcltest.lpr(37,36) Error: Error while compiling resources. Compile with -vd for more details. Check for duplicates. Any idea? Kostas ---------------------------------------------------------------------- meanderix · 2012-06-25 17:46 Quote from: Kostas on June 25, 2012, 07:12:56 pmBut when i compile for Android i get an error: androidlcltest.lpr(37,36) Error: Error while compiling resources. Compile with -vd for more details. Check for duplicates. Any idea? Hmm, I think I had a similar error -- I think I manually compiled it to a .res file and included that instead. It could be that you need to configure the resource compiler. ---------------------------------------------------------------------- Kostas · 2012-06-25 19:21 Quote from: meanderix on June 25, 2012, 07:46:06 pmHmm, I think I had a similar error -- I think I manually compiled it to a .res file and included that instead. It could be that you need to configure the resource compiler. Yes i converted first with brcc32 into a res file and included it in the exe. But when i try to open it with: aResStream := TResourceStream.Create(HInstance, 'PORTESDATA', 'RT_RCDATA'); i get a runtime error: Resource 'PORTESDATA' not found. The rc file that i created has the following line: PORTESDATA RCDATA "LeonPortes.coe" This happens in Win64. I haven't yet tested in Android... Any idea? EDIT: The resource file is added in the executable EDIT2: I am compiling in Win64 and run the program in a Windows7 64bit. No idea if this can be a problem... Thanks in advance Kostas ---------------------------------------------------------------------- meanderix · 2012-06-25 19:47 Quote from: Kostas on June 25, 2012, 09:21:40 pmThe rc file that i created has the following line: PORTESDATA RCDATA "LeonPortes.coe" The following worked for me: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SVGDATA STR tiger.svg  Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   S := TResourceStream.Create(HINSTANCE, PChar('SVGDATA'), PChar('STR'));  try    Doc.LoadFromStream(S);  finally    S.Free;  end;  I'm using the LResources unit. ---------------------------------------------------------------------- Kostas · 2012-06-26 05:35 The only way that worked for me is the use of lazarus resources. 1. I used lazres.exe to create a .lrs file: Code: [Select]         lazres network.lrs LeonPortes.coe 2. I included in the intialization section the .lrs file: Code: [Select]        {$I network.lrs} 3. I opened later the file using a TLazarusResourceStream: Code: [Select]        aLazRes := TLazarusResourceStream.Create('LeonPortes', 'COE'); Now it works right, also in Android! Kostas ---------------------------------------------------------------------- felipemdc · 2012-06-26 05:44 Quote from: meanderix on June 24, 2012, 03:28:32 pmI looked at this further and I don't think the raw files are extracted to /data/data/. I think you need to use getResources().openRawResource(R.raw.yourfile) to access the resource file (from the Java side.) It doesn't need to be from the Java side. You can make such a call (any call actually, now that I learned more about it) in JNI in Pascal. ---------------------------------------------------------------------- Daniello · 2012-06-26 12:35 Quote from: felipemdc on June 26, 2012, 07:44:51 amIt doesn't need to be from the Java side. You can make such a call (any call actually, now that I learned more about it) in JNI in Pascal. I'm also interested in loading files, could you post a sample that uses the JNI?