4

I am trying to create a file and write to it in the external directory. Due to scoped storage restriction in Android 11 I am not able to find a way to do it. The purpose of creating and writing to the file is just to log the data which I need and later view it using a file explorer or send to a specific mail.

Is it a proper way to go by the Official Docs even for logging the required data or is there any other recommended way to do it? As per the official docs It looks like it will prompt the user for the action but I feel it would be a bit odd using it for logs.

I have already tried the Crashlytics way of logging but it doesn't seem to appear in the Crashlytics of Firebase Console. Here is a reference I used for logging using Crashlytics.

Any Suggestions?

Joshua
  • 1,167
  • 1
  • 14
  • 32
  • "The purpose of creating and writing to the file is just to log the data which I need and later view it using a file explorer or send to a specific mail" -- for logging, and for sharing via `ACTION_SEND`, use `getFilesDir()` and `FileProvider`. Then, add an "export" or "backup" or some such option to your app, where you use `ACTION_CREATE_DOCUMENT` to allow the user to choose where that exported content should go, and copy the data from the file in `getFilesDir()` to the user-chosen location. – CommonsWare Dec 07 '20 at 18:40
  • @CommonsWare, yes, that's one way to do it. Is there any other way?.I was thinking about automating the logs to be sent to mail in future. So in that case, without the user action would be much better. – Joshua Dec 07 '20 at 18:47
  • "I was thinking about automating the logs to be sent to mail in future. So in that case, without the user action would be much better." -- use `getFilesDir()`. "automating the logs to be sent to mail" hopefully means that you are uploading the logs to your Web service, which will send the email. That does not require anything on external storage. – CommonsWare Dec 07 '20 at 19:01
  • @CommonsWare if we use getFilesDir(), the file will be deleted once user uninstall the app. is there a way we can avoid deleting these files.? – Anshul Kabra Apr 27 '21 at 09:22
  • @AnshulKabra: "if we use getFilesDir(), the file will be deleted once user uninstall the app" -- well, yes. In the context of this question, they are logs. They *really* belong in `getCacheDir()`, so the OS can flush them if the device gets low on disk space. "is there a way we can avoid deleting these files.?" -- use the Storage Access Framework (e.g., `ACTION_CREATE_DOCUMENT`, `ACTION_OPEN_DOCUMENT_TREE`), and allow the user to choose where on the user's device (or the user's cloud storage) that you should place the user's content. – CommonsWare Apr 27 '21 at 11:22

0 Answers0