Reading a file lazarus / 43-Android ---------------------------------------------------------------------- huberte · 2012-05-04 03:46 I try to read the content on a file Is it possible on android ? Like reading a file in a textbox (thanks Paul) Quoteprocedure TForm1.Button1Click(Sender: TObject); var   Stream: TFileStream;   S: AnsiString; begin   Stream := TFileStream.Create('1.txt', fmOpenRead or fmShareDenyNone);   try     SetLength(S, Stream.Size);     Stream.Read(S[1], Stream.Size);     LCLIntf.MessageBox(0, PAnsiChar(S), 'Caption', 0);   finally     Stream.Free;   end; end; If it is, I think I have to specify the path of the file. ---------------------------------------------------------------------- felipemdc · 2012-05-04 05:31 Yes, it should work, but you need to specify the full path to the file. ---------------------------------------------------------------------- huberte · 2012-05-04 10:37 The fulle Android path ? Cause with the windows path, I see in the logcat that Android serach the file in D:.... ---------------------------------------------------------------------- felipemdc · 2012-05-04 11:14 Yes, a full android path, for example "/sdcard/file.txt" ---------------------------------------------------------------------- huberte · 2012-05-04 14:13 Mh ok, but the apk cannot install those files in data for example ? Automatically ? ---------------------------------------------------------------------- felipemdc · 2012-05-04 14:29 It can, but I don't know the details since I never did that myself. Try Google or the Google Groups "Android Developers" as this is a generic Android question. But please also mention the solution here when you find it. ---------------------------------------------------------------------- huberte · 2012-05-05 02:44 the procedure mention above, that waht intend to have a text message withoiut button is crashing on some phone, I'll have to get the log. I don'y know why. Is there another "clean" solution to show such dialog on android ? Moreover, I did some search to load some extra file. it seems that I have to use getAssets(), which is in java implemanted, not when building the apk. If I do so, it appears that I could "break" some standard for the apk, and wonder how would google play react... ---------------------------------------------------------------------- felipemdc · 2012-05-05 10:55 Quote from: huberte on May 05, 2012, 04:44:37 amthe procedure mention above, that waht intend to have a text message withoiut button is crashing on some phone, I'll have to get the log. I don'y know why. Is there another "clean" solution to show such dialog on android ? So, are you saying that it works in some devices and in other devices not? Or it never works for you? I'd really like to set the logcat. MessageBox was implemented using Java code, so it is strange for me that it crashes. QuoteMoreover, I did some search to load some extra file. it seems that I have to use getAssets(), which is in java implemanted, not when building the apk. If I do so, it appears that I could "break" some standard for the apk, and wonder how would google play react... I think it is safe to use the real paths, I have never seen a device which does not use the standard paths. I think it is /data/data/com.pascal.lcltest/ or something like that, depending on your package name. You can use "adb shell" to navigate the file structure, but note that many operations require root access. ---------------------------------------------------------------------- huberte · 2012-05-07 05:21 QuoteI'd really like to set the logcat. MessageBox was implemented using Java code, so it is strange for me that it crashes. Here you have a working logcat (SGS2) and a non working logcat (HTC sensation). Morevover a friend have a SGS2 where the app crashed also. Ir crashed when pushing the menu button. QuoteYou can use "adb shell" to navigate the file structure, but note that many operations require root access. I can't publish an app with root access. Because I saw apps which are storing some files in data, I though it would be possible that my app could store txt files there too. ---------------------------------------------------------------------- felipemdc · 2012-05-07 05:28 QuoteI can't publish an app with root access. Because I saw apps which are storing some files in data, I though it would be possible that my app could store txt files there too. I was not talking about making the app require root access. I was just saying that if you root the device you can use "adb shell" more freely to learn the file structure of Android. This has nothing to do with your application. ---------------------------------------------------------------------- huberte · 2012-05-07 05:44 Oh sorry, so I maybe can add files to Data/data ? Moreover, I would like to close the form when I press the back button, because the app may use to much memory. I think I have to use FBackBtn but I dont know how. Did you see the logcat ? EDIT : - I think the app I sent you have such menus. - How do the programm compute labels, should I use them ? I have one or more charachers missing in middle of the line, when I go to line - How do I set a MainMenu without item since there is no onclick event on it ? (I did edit the .lfm file and add Quoteobject MainMenu1: TMainMenu   OnClick = TMainmenuClick does not work ---------------------------------------------------------------------- felipemdc · 2012-05-08 05:26 Quote from: huberte on May 07, 2012, 07:44:14 amOh sorry, so I maybe can add files to Data/data ? Yes. QuoteMoreover, I would like to close the form when I press the back button, because the app may use to much memory. I think I have to use FBackBtn but I dont know how. I haven't ever seen an app which will close itself when one hits the back button. It would be really strange in Android. Usually people add a close button to the app. In your case it could be a menu item for example. And even if there is no close button, people can install for example the "Advanced Task Killer" and use it to close the app. Moreover Android will auomatically close your app if it needs memory for something else. QuoteDid you see the logcat ? Yes, I am investigating. It takes some time. Quote- How do the programm compute labels, should I use them ? I have one or more charachers missing in middle of the line, when I go to line Could you show a screenshot? You can use the software ddms which comes with the Android SDK to make screenshots. Quote- How do I set a MainMenu without item since there is no onclick event on it ? Ummm .... maybe you will get the key in the event OnKeyDown of the element with focus. ---------------------------------------------------------------------- huberte · 2012-05-08 06:07 1. QuoteQuoteMoreover, I would like to close the form when I press the back button, because the app may use to much memory. I think I have to use FBackBtn but I dont know how. I haven't ever seen an app which will close itself when one hits the back button. It would be really strange in Android. I spoke about forms ! If one student opens multiple images, when using the back button, they all stay in memory, which is useless. The memory needed greatly increases, I checked that, about 50Mo. So I asked if, on the form where the image is, I could add a procedure, wich can kill the form or close it. Something like : procedure FBackBtn Form10.close; or with the ondestroy function ? 2. QuoteYes, I am investigating. It takes some time. thanks 3. After "différentielles" (missing s), like the "e" is cut. Very difficult to place on the foorm so it won't get cut on android. https://docs.google.com/open?id=0B86LlFFZdkAbWGtEQmZTeXRPU28 https://docs.google.com/open?id=0B86LlFFZdkAbS2NvSURwS2d0RzQ 4. QuoteUmmm .... maybe you will get the key in the event OnKeyDown of the element with focus. Impossible with my method I wrote above ! (modifying the .lfm, adding a onclick option ) I add TForm3.FormKeyDown on the form with LCLIntf.MessageBox works (what's the with focus ?) : it works ! but hiting the back button to the mainform and the msgbox still there. It's persistent, everywhere ---------------------------------------------------------------------- felipemdc · 2012-05-08 12:08 Quote from: huberte on May 08, 2012, 08:07:02 amI spoke about forms ! If one student opens multiple images, when using the back button, they all stay in memory, which is useless. The memory needed greatly increases, I checked that, about 50Mo. So I asked if, on the form where the image is, I could add a procedure, wich can kill the form or close it. Something like : procedure FBackBtn Form10.close; or with the ondestroy function ? Aha, in this case don't use auto-created forms but instead manage the memory yourself. Try something like this: procedure Form1.Button3Click; begin   Form2 := TForm2.Create(Self);   Form2.CloseAction := caFree;   Form2.Show; end; And remove Form2 creation from your auto-creation list. That list in the main library file which you asked if you need to put 100 forms there. You don't need to put anything except the mainform there. But if you don't put other forms there, then you need to create and handle their memory manually. QuoteImpossible with my method I wrote above ! (modifying the .lfm, adding a onclick option ) Yes, it is supposed to be like that. There is no event TMainMenu.OnClick simple like that. There is no such API. You have to use Form.OnKeyDown Also, never change the lfm manually like that unless you know very well what you are doing. If you add things which don't exist you might get crashes at startup. QuoteI add TForm3.FormKeyDown on the form with LCLIntf.MessageBox works (what's the with focus ?) : it works ! but hiting the back button to the mainform and the msgbox still there. It's persistent, everywhere  I don't understand what you are saying here. Please show some images and exact steps to reproduce. ---------------------------------------------------------------------- huberte · 2012-05-08 17:49 OK I will sum up. I have 3 forms, saying form0,1,2. On form1, have : QuoteTForm1 = class(TForm)     btnGrav: TButton;     btnOsc: TButton;     btnNewt: TButton;     btnChut: TButton;     btnEner: TButton;     procedure BtnNewtClick(Sender: TObject);     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);       private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1;   Form2: TForm2; implementation {$R *.lfm} { TForm3 } procedure TForm1.BtnNewtClick(Sender: TObject); begin   Form2 := TForm2.Create(Self);   Form2.Show; end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState   ); begin   LCLIntf.MessageBox(0, 'texte :' + sLineBreak +     'texte','about', 10000)       end; end.                      form2: Quotetype   { TForm2 }   TForm2 = class(TForm)     Image1: TImage;     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);     procedure FormCreate(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form2: TForm2; implementation {$R *.lfm} { TForm2 } procedure TForm2.FormCreate(Sender: TObject); begin   Image1.Width := Self.ClientWidth;   Image1.Height := Self.ClientHeight; end; procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin   CloseAction := caFree; end; end. Is that ok, for the free memory procedure ? I dont think so, cause i have multiple formcreate events with those, I have : - Still the image in the same state in my android app, eg, the form wont closeitself, stil lthe same amount of RAM after closing the second form. - The testbox coming from the menu button, still there when going back to form 0. Staying in form1, textbox is show when pushing the menu button, back and pushing again, etc... Gooing to form2 and back to form1, no text box when pushing the menu button.