Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Cannot open text file on IOS device



Permlink Replies: 3 - Last Post: Jan 13, 2015 5:30 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Stev Warby

Posts: 50
Registered: 10/19/14
Cannot open text file on IOS device
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 3:39 PM
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.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Cannot open text file on IOS device
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 4:21 PM   in response to: Stev Warby in response to: Stev Warby
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)
Stev Warby

Posts: 50
Registered: 10/19/14
Re: Cannot open text file on IOS device
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 4:33 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
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

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Cannot open text file on IOS device
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 5:30 PM   in response to: Stev Warby in response to: Stev Warby
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:

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;


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02