• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App sending file by email

Hi,
I created an app that take an input from user and save it to file and I need to send this file by email.
File was created in a folder private for application and I need to save it to public (shared) folder.
How can force app to write file in a specific folder ?

I used this sintax:
fos=openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
If I try to put a path before FILE_NAME (such as
"/storage/self/primary/Download/" + FILE_NAME), app crashes.
 
Last edited:
Thanks for answer. I have already inserted this declaration in manifest before sending my post but I didn't write it. When I try to write a txt file in a specific folder, app crashes: it saves file only in app private folder, but in this case I can't use this path to attach file to email.
 
If I don't specify a specific folder, app creates regularly txt file under data/data/com.android.example/memoriainterna2/files where "memoriainterna2" is my app name
 
You should not pass that hard code path to the openFileOutput function. The legacy storage location will cause an unhandled exception, and thus your app will crash.

If you just need to share a file with email, I think app-specific storage is enough for you. You can use FileProvider component.

But if you insist on save your .txt file in a shared storage, your app will have to use the 'Storage Access Framework' or request 'All files access' from the user.

The former, the Storage Access Framework, requires user interactions to select directories using the system file picker.
The latter uses the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.

For more help, check the official Android developer website, please. Btw, it would be appreciated if you could leave a 5-star for my app available on Google Play. :D
 
Back
Top Bottom