在Android上创建可公开访问的日志文件的位置(Where to create a publicly

编程入门 行业动态 更新时间:2024-10-19 12:39:03
在Android上创建可公开访问的日志文件的位置(Where to create a publicly-accessible log file on Android)

我们的应用程序有大量的C ++代码,可以将自己的日志文件创建为简单的.txt文件。 它是一个循环缓冲区,因此它仅限于我们指定的大小。 它也放在我们指定的任何目录中。

问题是放置文件的位置,以便可以使用ADB或类似工具访问它(无需生根)。 如果我们不关心可公开访问的部分,那么这似乎是找到该文件的合理位置:

packageManager.getApplicationInfo(applicationContext.getPackageName().dataDir

但由于我们希望能够从客户的手机中提取文件以进行事后调试,我尝试将其放在此处:

"/mnt/sdcard/Android/data"

由于几个原因,这是有问题的,但我不确定它们是否都是真的。 (1)它是硬编码的; (2)并非所有Android设备都有外部存储,虽然我认为它们仍然将其映射到内部存储? (3)该位置不是特定于应用程序的,因此它不会与应用程序一起卸载。 并且(4)需要EXTERNAL_STORAGE的运行时权限。

我相信1-3可以通过以下方式解决:

android.content.Context.getExternalFilesDir()

还是有更好的选择?

但我不相信这会绕过#4,这是不幸的,因为我不想用更多的权限请求“吓唬”用户。

处理这个问题的最佳方法是什么?

Our application has a large amount of C++ code that creates its own log file as a simple .txt file. It's a circular buffer so it's limited to the size we specify. It's also placed in whatever directory we specify.

The problem is where to place the file so it can be accessed with ADB or a similar tool (without rooting). If we didn't care about the publicly-accessible part, it seems this would be the logical place to locate the file:

packageManager.getApplicationInfo(applicationContext.getPackageName().dataDir

But since we want to be able to pull the file from a customer's phone for post-mortem debugging, I've tried placing it here:

"/mnt/sdcard/Android/data"

This is problematic for several reasons, but I'm not sure if they're all true. (1) It's hard-coded; (2) Not all Android devices have external storage, although I thought they still mapped it to internal storage? (3) The location isn't app-specific so it won't get uninstalled along with the app. And (4) Runtime permission for EXTERNAL_STORAGE is required.

I believe 1-3 can be solved with something like:

android.content.Context.getExternalFilesDir()

Or is there a better choice?

But I don't believe this will get around #4, which is unfortunate as I'd prefer not to "scare" users with more permission requests.

What's the best way to handle this?

最满意答案

确保您具有使用清单文件中的此代码读取和写入外部SD的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

然后这个字符串将为您提供所需的路径:

String directory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + getContext().getPackageName();

/storage/emulated/0/Android/data/com.exemple.yourapp/

Make sure that you have the permissions to read and write the External SD using this code in the Manifest File:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And then this string will give you the wanted path:

String directory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + getContext().getPackageName();

/storage/emulated/0/Android/data/com.exemple.yourapp/

更多推荐

文件,file,Android,问题,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-08-07 06:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   文件   日志   Android   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!