Bungles Bungles - 4 months ago 35
C++ Question

Where to create a publicly-accessible log file on Android

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:


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:


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:


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?


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();