Hi, Try using content providers.
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ fileName));
........ Android: link files from the internal cache to Gmail
package com.stephendnicholas.gmailattach; import java.io.File; import java.io.FileNotFoundException; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.net.Uri; import android.os.ParcelFileDescriptor; import android.util.Log; public class CachedFileProvider extends ContentProvider { private static final String CLASS_NAME = "CachedFileProvider";
<provider android:name="CachedFileProvider" android:authorities="com.stephendnicholas.gmailattach.provider">
public static void createCachedFile(Context context, String fileName, String content) throws IOException { File cacheFile = new File(context.getCacheDir() + File.separator + fileName); cacheFile.createNewFile(); FileOutputStream fos = new FileOutputStream(cacheFile); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8"); PrintWriter pw = new PrintWriter(osw); pw.println(content); pw.flush(); pw.close(); }
public static Intent getSendEmailIntent(Context context, String email, String subject, String body, String fileName) { final Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND); //Explicitly only use Gmail to send emailIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail"); emailIntent.setType("plain/text"); //Add the recipients emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { email }); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); //Add the attachment by specifying a reference to our custom ContentProvider //and the specific file of interest emailIntent.putExtra( Intent.EXTRA_STREAM, Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" + fileName)); return emailIntent; }
http://stephendnicholas.com/archives/974#comment-342
source share