Mega Search
23.2 Million


Sign Up

Make a donation  
Cannot open text file on IOS device  
News Group: embarcadero.public.delphi.platformspecific.ios

I am able to savetofile a memo and load it back again in windows.
In IOS I can save the memo (I see the files in XCode and they have data) but when trying to load them I get error . See links

 [www.uglyapp.com/delphi/IMG_0006.PNG ]  [www.uglyapp.com/delphi/IMG_0007.PNG ] 

My code is :

procedure TTabbedForm.btnSaveQuoteClick(Sender: TObject);
var
AppPath : String;

begin

    MessageDlg('Save this Quote', System.UITypes.TMsgDlgType.mtInformation,

    [

      System.UITypes.TMsgDlgBtn.mbYes,

      System.UITypes.TMsgDlgBtn.mbNo,

      System.UITypes.TMsgDlgBtn.mbCancel

    ], 0,



      // Use an anonymous method to make sure the acknowledgment appears as expected.



      procedure(const AResult: TModalResult)

      begin

        case AResult

          of

          { Detect which button was pushed and show a different message }

          mrYES:
            begin
            //ShowMessage('You chose Yes');
             memoQuote.Lines.Add(' ');
    memoQuote.Lines.Add(' ');
    memoQuote.Lines.Add(' ');
    memoQuote.Lines.Add('Total ' + editTotal.Text);
    memoQuote.Lines.Add(' ');
    memoQuote.Lines.Insert(1, DateToStr(Date));
    memoQuote.Lines.Add(lastLineText);
    memoQuote.SetFocus;

        {$IF DEFINED(MSWINDOWS)}

    //memoQuote.Lines.Add(selectedCompanyIndex);

    if Not TDirectory.Exists('quotes') then
    TDirectory.CreateDirectory('quotes');
    memoQuote.Lines.SaveToFile('quotes\'+ editCompanyIndex.Text + editQuoteNo.Text+'.txt');
    //clearQuote();
    {$ENDIF}

    {$IF DEFINED(iOS) or DEFINED(ANDROID)}


    AppPath := TPath.GetDocumentsPath + PathDelim;
    memoQuote.Lines.SaveToFile(AppPath+  editCompanyIndex.Text + editQuoteNo.Text+'.txt');
    //clearQuote();
    {$ENDIF}

    TabItem2.Enabled := false;
    TabControl1.ActiveTab := TabItem3;
 // AValue is the result of the inputbox dialog

  // Show the button type selected
          end;


          mrCancel:

            //ShowMessage('You chose Cancel');

        end;



      end



    );






end;


procedure TTabbedForm.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
  var
  filename, companyIndex, AppPath: String;
begin
    showmessage(AItem.Text);
    {$IF DEFINED(MSWINDOWS)}
    memoQuote.Lines.LoadFromFile(AItem.Text);
    filename := ExtractFileName(AItem.Text);
    companyIndex := Copy(filename, 1, 1);
    ImageControl2.LoadFromFile('company'+companyIndex+'.jpg');

      {$ENDIF}
    {$IF DEFINED(iOS) or DEFINED(ANDROID)}
    AppPath := TPath.GetDocumentsPath + PathDelim ;
    showmessage(AppPath+AItem.Text);
    memoQuote.Lines.LoadFromFile(AppPath+AItem.Text);
    filename := ExtractFileName(AItem.Text);
    companyIndex := Copy(filename, 1, 1);
    Showmessage(AppPath+'company'+companyIndex+'.jpg');
    ImageControl2.LoadFromFile(AppPath+'company'+companyIndex+'.jpg');
    {$ENDIF}
     TabControl1.ActiveTab := TabItem3;
end;

I have also uploaded images and can load them into TImage and a sqlite database file using the same AppPath syntax as above ie:

procedure TTabbedForm.Button10Click(Sender: TObject);
var
AppPath , FileName , imageFilename : String;
begin

          {$IF DEFINED(MSWINDOWS)}
      //AppPath := TPath.GetDocumentsPath + PathDelim;
      //Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + '36X36.png');
      FileName := 'stoves2.sdb';
      imageFilename := '36X36.png';
      StovesConnection.Params.Values['Database'] := FileName;
      ImageControl1.LoadFromFile(imageFilename);
      //showmessage(FileName + '   ' + imageFilename);
  {$ENDIF}
          {$IF DEFINED(iOS) or DEFINED(ANDROID)}
      AppPath := TPath.GetDocumentsPath + PathDelim;
      //Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + '36X36.png');
      FileName := TPath.Combine(AppPath, 'stoves2.sdb');
      imageFilename := TPath.Combine(AppPath, '36X36.png');
      StovesConnection.Params.Values['Database'] := FileName;
      ImageControl1.LoadFromFile(imageFilename);
      //showmessage(FileName + '   ' + imageFilename);
  {$ENDIF}
    //showmessage(TPath.GetLibraryPath);
end;




Can anyone see anything wrong with the paths or am I doing something wrong ?


Thanks.

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 3:39 PM EST
From: Stev Warby
 
Re: Cannot open text file on IOS device  
News Group: embarcadero.public.delphi.platformspecific.ios
Stev wrote:

> Got it.... Too many late nights and IF DEFS

In this particular example, I would suggest getting rid of the IFDEFs altogether 
if you can make your Windows code use TPath system folders.

Otherwise, at least reduce the code's dependancy on the IFDEFs, since the 
bulk of your IFDEF'ed code is duplicated across the multiple platforms so 
it does not need to be IFDEF'ed in the first place, eg:

{code}
procedure TTabbedForm.btnSaveQuoteClick(Sender: TObject);
var
  AppPath : String;
begin
  MessageDlg('Save this Quote', System.UITypes.TMsgDlgType.mtInformation,
    [System.UITypes.TMsgDlgBtn.mbYes, System.UITypes.TMsgDlgBtn.mbNo, System.UITypes.TMsgDlgBtn.mbCancel], 
0,

    // Use an anonymous method to make sure the acknowledgment appears as 
expected.
    procedure(const AResult: TModalResult)
    begin
      case AResult of
        { Detect which button was pushed and show a different message }

        mrYES:
        begin
          //ShowMessage('You chose Yes');
          memoQuote.Lines.Add(' ');
          memoQuote.Lines.Add(' ');
          memoQuote.Lines.Add(' ');
          memoQuote.Lines.Add('Total ' + editTotal.Text);
          memoQuote.Lines.Add(' ');
          memoQuote.Lines.Insert(1, DateToStr(Date));
          memoQuote.Lines.Add(lastLineText);
          memoQuote.SetFocus;

          {$IF DEFINED(MSWINDOWS)}
          AppPath := 'quotes'; // TODO: use an absolute path instead, even 
a system folder from TPath
          if not TDirectory.Exists(AppPath) then
            TDirectory.CreateDirectory(AppPath);
          {$ELSE}
          AppPath := TPath.GetDocumentsPath;
          {$ENDIF}
 
          memoQuote.Lines.SaveToFile(TPath.Combine(AppPath, editCompanyIndex.Text 
+ editQuoteNo.Text + '.txt'));
          //clearQuote();

          TabItem2.Enabled := false;
          TabControl1.ActiveTab := TabItem3;
          // AValue is the result of the inputbox dialog

          // Show the button type selected
        end;

        mrCancel:
          //ShowMessage('You chose Cancel');

      end;
    end
  );
end;

procedure TTabbedForm.ListView1ItemClick(const Sender: TObject; const AItem: 
TListViewItem);
var
  filename, companyIndex, AppPath: String;
begin
  ShowMessage(AItem.Text);

  {$IF DEFINED(MSWINDOWS)}
  AppPath := ''; // TODO: use an absolute path instead, even a system folder 
from TPath
  {$ELSE}
  AppPath := TPath.GetDocumentsPath;
  {$ENDIF}

  memoQuote.Lines.LoadFromFile(AItem.Text);
  filename := ExtractFileName(AItem.Text);
  companyIndex := Copy(filename, 1, 1);
  ImageControl2.LoadFromFile(TPath.Combine(AppPath, 'company' + companyIndex 
+ '.jpg'));

  TabControl1.ActiveTab := TabItem3;
end;

procedure TTabbedForm.Button10Click(Sender: TObject);
var
  AppPath , FileName , imageFilename : String;
begin
  {$IF DEFINED(MSWINDOWS)}
  AppPath := ''; // TODO: use an absolute path instead, even a system folder 
from TPath
  {$ELSE}
  AppPath := TPath.GetDocumentsPath;
  {$ENDIF}

  //Image1.Bitmap.LoadFromFile(TPath.Combine(AppPath, '36X36.png'));
  FileName := TPath.Combine(AppPath, 'stoves2.sdb');
  imageFilename := TPath.Combine(AppPath, '36X36.png');
  StovesConnection.Params.Values['Database'] := FileName;
  ImageControl1.LoadFromFile(imageFilename);
  //ShowMessage(FileName + ' ' + imageFilename);
  //ShowMessage(TPath.GetLibraryPath);
end;
{code}

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 5:30 PM EST
From: Remy Lebeau (TeamB)
 
Re: Cannot open text file on IOS device  
News Group: embarcadero.public.delphi.platformspecific.ios
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Stev wrote:
> 
> > I am able to savetofile a memo and load it back again in windows.
> > 
> > In IOS I can save the memo (I see the files in XCode and they have
> > data) but when trying to load them I get error .
> 
> In ListView1ItemClick(), TListViewItem.Text contains a file path.  On Windows, 
> you are using that path as-is, which is why the load works.  But on iOS/Android, 
> you are prepending the path with TPath.GetDocumentsPath(), which is why iOS/Android 
> cannot find the file.
> 
> You are not making that same mistake when saving the .txt files (or when 
> loading the image files).
> 
> --
> Remy Lebeau (TeamB)

Got it.... Too many late nights and IF DEFS


Thanks

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 4:33 PM EST
From: Stev Warby
 
Re: Cannot open text file on IOS device  
News Group: embarcadero.public.delphi.platformspecific.ios
Stev wrote:

> I am able to savetofile a memo and load it back again in windows.
> 
> In IOS I can save the memo (I see the files in XCode and they have
> data) but when trying to load them I get error .

In ListView1ItemClick(), TListViewItem.Text contains a file path.  On Windows, 
you are using that path as-is, which is why the load works.  But on iOS/Android, 
you are prepending the path with TPath.GetDocumentsPath(), which is why iOS/Android 
cannot find the file.

You are not making that same mistake when saving the .txt files (or when 
loading the image files).

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 4:21 PM EST
From: Remy Lebeau (TeamB)