android - 写一些文字并保存在我的安卓手机上

标签 android delphi firemonkey

我试图在我的 Android 手机上的文件 (test.txt) 上保存一些文本,但没有成功。这是我的代码示例:

procedure TDM.WriteLog(text:string);
var
   myFile : TextFile;


 begin


   // Try to open the Test.txt file for writing to
   AssignFile(myFile, System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt'));
   ReWrite(myFile);

   // Write a couple of well known words to this file
   WriteLn(myFile, text + sLineBreak );


   // Close the file
   CloseFile(myFile);


 end;

当我在手机上找到文件时,我打开了文件,但它是空的。我错过了什么?

最佳答案

当你想在 Android 中创建一个文本文件时(当然 iOS 和 macOS 也是如此)你可以使用 WriteAllText属于 TFile 类的类过程。您必须在 uses 子句中添加 System.IOUtils

TFile.WriteAllText(TPath.Combine(TPath.GetDocumentsPath, 'test.txt'), 'content');

当您要存储文本文件或与您的应用程序相关的一般数据时,建议您使用 GetDocumentsPath。这是一种创建文本文件的现代方法,但不是唯一的方法;还请考虑您可以:

  1. 使用 TStringlist,然后调用 SaveToFile('path')
  2. 如果您使用的是备忘录,它具有 SaveToFile('path') 功能
  3. 使用TStreamWriter类(以及用于读取内容的 StreamReader)

关于android - 写一些文字并保存在我的安卓手机上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098458/

相关文章:

arrays - 如果可以估计的话,TList、TObjectList 和普通数组之间的性能差异有多明显?

delphi - 如何从 FireMonkey TListBox 控件中删除边界线?

delphi - Firemonkey 组件在运行时移动

macos - 使用 Delphi Firemonkey 应用程序让 firebird 在 OSX 下工作

android - Xamarin android 内存消耗在使用量达到一定阈值后无限增长

android - 边距属性在自定义 DialogFragment 中不起作用

delphi - Delphi中MVC中如何避免循环通知?

macos - Delphi 10.3.2 Catalina 公证失败,但 Apple 确实不公证

java - 我如何从 Android 上的相机选择的图像中获得正确的宽度和高度?

android - 了解 GridView 中的当前单元格