• 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:
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
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones