1) Now, please modify the code to add the lines in reversed order (last added line to be shown first in ListView).
This is easy. Just change line where is
ListView1.Add(s); to:
2) After that, please make me understand how to create the file test.dat in other path (location) different from GetFilePath(fpathData) + '\test.dat', let's say on the root of SdCard (something like this SdCard+'\test.dat', and then read test.dat from this different path (location).
This is a bit trickier. Problem is that Android limits access to data outside your app for security. I use
RequestOpenFile procedure and then copy bytes from chosen file to local app storage. There you can read/write files without restriction. There are (probably) other ways to do it, but this approach is enough for me.
To do it, put component jIntentManager to your form. Add following constants somewhere above methods that use them:
const cRCOpenFile = 1001;
cFileName = '\test.dat';
Change Button1 click method to:
procedure TAndroidModule1.Button1Click(Sender: TObject);
begin
RequestOpenFile(GetEnvironmentDirectoryPath(dirDownloads), 'text/plain', cRCOpenFile); // or: dirDocuments
end;
Put following code in OnActivityResult method of the form:
procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject; requestCode: integer;
resultCode: TAndroidResult; intentData: jObject);
var Uri: jObject;
BytesFromUri: TDynArrayOfJByte;
fs: TFileStream;
begin
If (resultCode = RESULT_OK) and (requestCode = cRCOpenFile) then
begin
Uri := IntentManager1.GetDataUri(intentData);
// write file from Uri to app data directory (where you can read/write as usual)
BytesFromUri := LoadBytesFromUri(Uri);
fs := TFileStream.Create(GetFilePath(fpathData) + cFileName, fmCreate);
try
fs.Seek(0, soFromBeginning);
fs.Write(BytesFromUri[0], Length(BytesFromUri));
finally
fs.Free;
SetLength(BytesFromUri, 0);
end;
end;
end;
When you press Button1 you'll get to choose file. After choosing file is copied to your app data directory (so you don't have to choose file each time).
I attached files from project.