Reputation: 149
My app writes data to text files (on sd card and internal memory). Later the app emails the text files to a list of people. I am having trouble getting gmail to attach a file that is pulled from the internal application files area. 'Native Android mail' can attach a file from either internal or SD card area with no problem. Gmail will attach a file if it's from SD card, but won't attach a file if its located in internal storage.
// this sends a file from SD - works for android mail and gmail
Intent jj=new Intent(android.content.Intent.ACTION_SEND);
String fileName = "file://" + Environment.getExternalStorageDirectory()+"/aFolder/externalfile.txt"
jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
jj.setType("text/plain");
Intent chooser = Intent.createChooser(jj , "Select Sender");
startActivity(chooser);
// this sends an internal file-works for android mail, but no attachment sent with gmail
Intent jj=new Intent(android.content.Intent.ACTION_SEND);
String fileName = "file://" + getFilesDir().toString() + "/internalfile.txt";
jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
jj.setType("text/plain");
Intent chooser = Intent.createChooser(jj , "Select Sender");
startActivity(chooser);
Any suggestions?
Do I need to give Gmail special permission somehow?
My attachments are all text files - written by the app.
Internal files were created with openFileOutput(myFile,32769)
Thanks John D
Upvotes: 2
Views: 6395
Reputation: 41
Like Dhego, I used a content provider. Specifically, a FileProvider.
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
Using this only requires that you modify your Manifest and create an additional resource file. Also, you will need to obtain a validly formatted URI via a static method provided by FileProvider.
In your Manfiest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.authority.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
The "com.example.authority.fileprovider" is your application authority with "fileprovider" appended.
In the res/xml folder, create a file_paths.xml file that contains the paths to the files you want to expose. In my case, I was exposing them from the application cache directory, so my XML looks like:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="reports" path="reports/"/>
</paths>
In my case, "reports" is a folder within the application cache directory that I am writing files to.
The last thing to do is in your code:
Here's some sample code:
emailIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), "com.example.authority.fileprovider", fileToAttach));
Invoke startActivity on your Intent and that should be it!
Upvotes: 1
Reputation: 1081
It appears that there is indeed an issue with gmail. Unfortunately however, at the time of the writing it seems it hasn't been fixed.
Upvotes: 0
Reputation: 59
the only way I found around this was to make my own content provider and pass in the uri to my content provider as the attachment.
Upvotes: 2