Similar threads here do not have answers that helped... I want to create email message with file attach, file is on internal storage. Here is code:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{email});
i.putExtra(Intent.EXTRA_SUBJECT, "Smart Weight Tracker");
i.putExtra(Intent.EXTRA_TEXT , "CSV file is in attachment");
Uri uri;
if(useExternal){
uri = Uri.fromFile(new File(this.getExternalFilesDir(null),fname));
}
else{
File f = new File(this.getFilesDir(),fname);
f.setReadable(true, false);
f.setWritable(true, false);
uri = Uri.fromFile(f);
}
i.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
It works perfectly with external storage, but i have no attach when use internal storage. I checked - file opens, (it's length in my app by showing Toast - is OK, > 0).
I write it this way:
OutputStreamWriter out =
new OutputStreamWriter(con.openFileOutput(fname, Context.MODE_WORLD_READABLE));
In logcat i see
I/Gmail (28480): >>>>> Attachment uri: file:///data/data/Android.MyApp/files /31.10.2011.csv
I/Gmail (28480): >>>>> type: text/plain
I/Gmail (28480): >>>>> name: 31.10.2011.csv
I/Gmail (28480): >>>>> size: 0
Size == 0!
Any ideas?
Please try this code. Hope this would help.
Hi, Try to use content providers.
........ Android: Attaching files from internal cache to Gmail
<provider android:name="CachedFileProvider" android:authorities="com.stephendnicholas.gmailattach.provider">
http://stephendnicholas.com/archives/974#comment-342
I was following the stephendnicholas code for hours thinking I HAD to write my files to internal storage because my device had no sd card.
Creating my "internal" file like this worked just fine for me:
It avoided Gmail limitations during attachment.
Before this I was creating my file object like this:
But if your file is literally from internal storage, then stephendnicholas content providers etc. work if you have mad android skills.