1. Are you ready for the Galaxy S20? Here is everything we know so far!

App sending file by email

Discussion in 'Android Development' started by Luca Franceschini, Jan 13, 2022.

  1. Luca Franceschini

    Thread Starter

    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.
     


    #1 Luca Franceschini, Jan 13, 2022
    Last edited: Jan 13, 2022

    1. Download the Forums for Android™ app!


      Download

       
  2. James_Watson

    James_Watson Android Enthusiast

    I think you should declare the MANAGE_EXTERNAL_STORAGE permission in the manifest at first.
     
  3. Luca Franceschini

    Thread Starter

    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.
     
  4. Luca Franceschini

    Thread Starter

    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
     
  5. Luca Franceschini

    Thread Starter

    In this post I attach my Android Manifest and file java for app.
     

    Attached Files:

  6. Luca Franceschini

    Thread Starter

    Thanks for answer, last friday I posted my Manifest file and also Java file used in my app, could you tell me if there something wrong ?
     
  7. James_Watson

    James_Watson Android Enthusiast

    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
     
Loading...

Share This Page

Loading...