在发布的Android应用中将输出写入日志文件?

huangapple go评论100阅读模式
英文:

Write output to log file in released Android app?

问题

我想要在Android应用程序中将printf()类型的输出写入日志文件。
这应该发生在我的最终用户手机上运行的已发布apk文件中。
谷歌提供的大部分示例都是针对开发人员正在测试的调试apk文件,这不是我要问的。
一些示例还建议将日志写入SD卡,但现在Pixel设备不再具有外部存储,只有内部存储。
所以我问这个问题是因为10年前的答案不再有效。
如何使用Kotlin在Android手机上以编程方式写入logfile.txt?
这个文件的位置应该在哪里,以便Android不会自动删除它?

英文:

I want to write printf() type outputs to a log file in Android app.
This should happen for the released apk file running on my end users phones.
The vast examples from Google are for debug apk file which the developer is testing and this is not what I am asking for.
Some examples also suggest writing to SD card, but these days Pixel devices do not have external storage, only insternal storage.
So I am asking this because answers from 10 years ago are no longer valid.
How to write to a logfile.txt in Android phones programatically using Kotlin?
Where should the location of this file beso that Android does not automatically delete it?

答案1

得分: 1

I want to write printf() type outputs to a log file in Android app. This should happen for the released apk file running on my end users' phones.

你想要在Android应用程序中将printf()类型的输出写入日志文件。这应该发生在我的最终用户手机上运行的已发布的apk文件中。

You might prefer a hosted logging solution.

您可能更喜欢托管的日志记录解决方案。

Some examples also suggest writing to SD card, but these days Pixel devices do not have external storage, only internal storage.

一些示例还建议将日志写入SD卡,但现在Pixel设备没有外部存储,只有内部存储。

All Android devices have external storage, at least as the Android SDK uses the term.

所有Android设备都有外部存储,至少根据Android SDK的术语来说是这样的。

How to write to a logfile.txt in Android phones programmatically using Kotlin? Where should the location of this file be so that Android does not automatically delete it?

如何在Android手机上使用Kotlin编程将数据写入logfile.txt?该文件的位置应该在哪里,以便Android不会自动删除它?

Use standard Java file I/O. If you only need your app to have access to the logs, use getFilesDir() on Context to get a good directory to use. If you want your user to have an option of accessing the logs, use getExternalFilesDir() on Context to get a good directory to use.

使用标准的Java文件I/O。如果您只需要您的应用程序访问日志,可以在Context上使用getFilesDir()来获取一个好的目录。如果您希望用户可以选择访问日志,可以在Context上使用getExternalFilesDir()来获取一个好的目录。

英文:

> I want to write printf() type outputs to a log file in Android app. This should happen for the released apk file running on my end users phones.

You might prefer a hosted logging solution.

> Some examples also suggest writing to SD card, but these days Pixel devices do not have external storage, only insternal storage

All Android devices have external storage, at least as the Android SDK uses the term.

> How to write to a logfile.txt in Android phones programatically using Kotlin? Where should the location of this file beso that Android does not automatically delete it?

Use standard Java file I/O. If you only need your app to have access to the logs, use getFilesDir() on Context to get a good directory to use. If you want your user to have an option of accessing the logs, use getExternalFilesDir() onContext to get a good directory to use.

huangapple
  • 本文由 发表于 2023年8月10日 19:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定