Recent

Author Topic: How to load a binary file from the resources  (Read 16386 times)

Kostas

  • New Member
  • *
  • Posts: 30
How to load a binary file from the resources
« on: June 10, 2012, 11:02:07 am »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: How to load a binary file from the resources
« Reply #1 on: June 10, 2012, 02:29:54 pm »
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/<package name> 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

  • New Member
  • *
  • Posts: 30
Re: How to load a binary file from the resources
« Reply #2 on: June 10, 2012, 05:06:42 pm »
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

  • Jr. Member
  • **
  • Posts: 50
Re: How to load a binary file from the resources
« Reply #3 on: June 14, 2012, 03:49:42 pm »
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?

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

  • Jr. Member
  • **
  • Posts: 50
Re: How to load a binary file from the resources
« Reply #4 on: June 24, 2012, 03:28:32 pm »
I looked at this further and I don't think the raw files are extracted to /data/data/<packagename>. 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.
« Last Edit: June 24, 2012, 03:30:22 pm by meanderix »

Kostas

  • New Member
  • *
  • Posts: 30
Re: How to load a binary file from the resources
« Reply #5 on: June 25, 2012, 07:12:56 pm »
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.
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

  • Jr. Member
  • **
  • Posts: 50
Re: How to load a binary file from the resources
« Reply #6 on: June 25, 2012, 07:46:06 pm »
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?
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

  • New Member
  • *
  • Posts: 30
Re: How to load a binary file from the resources
« Reply #7 on: June 25, 2012, 09:21:40 pm »
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.
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
« Last Edit: June 25, 2012, 09:45:01 pm by Kostas »

meanderix

  • Jr. Member
  • **
  • Posts: 50
Re: How to load a binary file from the resources
« Reply #8 on: June 25, 2012, 09:47:45 pm »
The rc file that i created has the following line: PORTESDATA RCDATA "LeonPortes.coe"

The following worked for me:

Code: Text  [Select][+][-]
  1. SVGDATA STR tiger.svg
  2.  

Code: Text  [Select][+][-]
  1.   S := TResourceStream.Create(HINSTANCE, PChar('SVGDATA'), PChar('STR'));
  2.   try
  3.     Doc.LoadFromStream(S);
  4.   finally
  5.     S.Free;
  6.   end;
  7.  

I'm using the LResources unit.
« Last Edit: June 25, 2012, 09:49:25 pm by meanderix »

Kostas

  • New Member
  • *
  • Posts: 30
Re: How to load a binary file from the resources
« Reply #9 on: June 26, 2012, 07:35:37 am »
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
« Last Edit: June 26, 2012, 07:44:29 am by Kostas »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: How to load a binary file from the resources
« Reply #10 on: June 26, 2012, 07:44:51 am »
I looked at this further and I don't think the raw files are extracted to /data/data/<packagename>. 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

  • Jr. Member
  • **
  • Posts: 64
Re: How to load a binary file from the resources
« Reply #11 on: June 26, 2012, 02:35:07 pm »
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.
I'm also interested in loading files, could you post a sample that uses the JNI?

 

TinyPortal © 2005-2018