Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi FireMonkey store data inside application

I need to store some custom, static data inside my Delphi XE5 FireMonkey (Android) mobile application. I have seen a 'RC_DATA' solution on the web, but it is windows-specific. I need a cross-platform solution.

Here is the RC_DATA solution I discovered:http://delphidabbler.com/articles?article=2

A comment in the code sample reads: // resource type - RT_RCDATA (from Windows unit)

I concluded from this comment that the code inside the Windows unit was Windows-specific. I apologize if this is not the case.

Since Chris said that it is, in fact, cross-platform, I will try it out. Thank you Chris.

With respect to asking for a component or library, I have read and re-read my question and I just don't see it. I guess a 'solution' COULD POSSIBLY include a component or library, but that was never in my mind. Again, I apologize if I did something wrong.

like image 509
user3224814 Avatar asked Jan 22 '14 19:01

user3224814


1 Answers

Far from what you call the '"RC_DATA" solution' being Windows-specific, it is actually the most straight-forwardly cross platform way Delphi provides. What you need to do is to add the files to the project via Project|Resources and Images...; since the other resource types are Windows-specific, just stick to RCDATA for each one. Then, to load, use TResourceStream like you might have done in a VCL application (RT_RCDATA is declared in System.Types):

procedure LoadStringsFromRes(Strings: TStrings; const ResName: string);
var
  Stream: TResourceStream;
begin
  Stream := TResourceStream.Create(HInstance, ResName, RT_RCDATA);
  try
    Strings.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

Also, if for some reason you want to check whether a given resource exists before trying to load it, the System unit implements the Windows API FindResource function for non-Windows platforms.

An alternative to embedded resources is to use Project|Deployment to add files to the application bundle (OS X and iOS) or APK (Android). While this might be considered a more 'platform native' way of doing things, the appropriate 'remote path' is platform-specific... and frankly, if a person is so obsessed at being platform-native they can't handle embedded resources, then they wouldn't be using FMX in the first place...


Update: with regard to the article that has now been linked to, it's showing how to write out a RES file manually, which isn't what a person would normally do - if you aren't using the IDE's little resource manager, then the normal way is to create a RC script and compile it with either the Borland or Microsoft resource compiler.

like image 102
Chris Rolliston Avatar answered Nov 05 '22 02:11

Chris Rolliston