Recent

Author Topic: Reading a file  (Read 8738 times)

huberte

  • Jr. Member
  • **
  • Posts: 78
Reading a file
« on: May 04, 2012, 05:46:35 am »
I try to read the content on a file

Is it possible on android ?

Like reading a file in a textbox (thanks Paul)

Quote
procedure 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. 
« Last Edit: May 04, 2012, 06:13:10 am by huberte »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #1 on: May 04, 2012, 07:31:37 am »
Yes, it should work, but you need to specify the full path to the file.

huberte

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #2 on: May 04, 2012, 12:37:29 pm »
The fulle Android path ? Cause with the windows path, I see in the logcat that Android serach the file in D:....

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #3 on: May 04, 2012, 01:14:12 pm »
Yes, a full android path, for example "/sdcard/file.txt"

huberte

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #4 on: May 04, 2012, 04:13:33 pm »
Mh ok, but the apk cannot install those files in data for example ? Automatically ?

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #5 on: May 04, 2012, 04:29:49 pm »
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

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #6 on: May 05, 2012, 04:44:37 am »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #7 on: May 05, 2012, 12:55:29 pm »
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 ?

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.

Quote
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...

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

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #8 on: May 07, 2012, 07:21:56 am »
Quote
I'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.

Quote
You 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

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #9 on: May 07, 2012, 07:28:49 am »
Quote
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.

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

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #10 on: May 07, 2012, 07:44:14 am »
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
Quote
object MainMenu1: TMainMenu
  OnClick = TMainmenuClick
does not work
« Last Edit: May 08, 2012, 07:23:53 am by huberte »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #11 on: May 08, 2012, 07:26:33 am »
Oh sorry, so I maybe can add files to Data/data ?

Yes.

Quote
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.

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.

Quote
Did 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

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #12 on: May 08, 2012, 08:07:02 am »
1.

Quote
Quote
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.

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.
Quote
Yes, 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.
Quote
Ummm .... 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

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: Reading a file
« Reply #13 on: May 08, 2012, 02:08:16 pm »
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 ?

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.

Quote
Impossible 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.

Quote
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  :-\

I don't understand what you are saying here. Please show some images and exact steps to reproduce.

huberte

  • Jr. Member
  • **
  • Posts: 78
Re: Reading a file
« Reply #14 on: May 08, 2012, 07:49:32 pm »
OK I will sum up.

I have 3 forms, saying form0,1,2.

On form1, have :
Quote
TForm1 = 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:
Quote
type

  { 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.

 

TinyPortal © 2005-2018