17

The email is being received on by the recipient, but without the attachment. Here is the code, any expert knows where did I go wrong?

 Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);

String aEmailList[] = { "mymailgmail.com" };
messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);

messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
...    
messageIntent.setType("image/jpeg");
File downloadedPic = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyApp.jpg");

messageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));

startActivity(Intent.createChooser(messageIntent, getResources().getString(R.string.chooser_pic)));

I get:

file:// attachment path must point to file://sdcard. Ignoring attachment file://...file name is MyApp.jpg

I am not getting image, only above text message. Thanks.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Kumar
  • 623
  • 3
  • 6
  • 16
  • same issue http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya Aug 06 '15 at 05:36

3 Answers3

40

Try below code...

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App"); 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/Myimage.jpeg"));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • 4
    I tried using it, but the attachment is missing in the received email. Any idea why is it so? – Amrit Apr 30 '14 at 11:44
  • Check your path of attachment file.. i think it may be wrong. – Mehul Ranpara Apr 30 '14 at 12:11
  • path is correct and the file is also seen as attached with the email on sender's end. But once the email is sent, attachment not visible neither on sender nor receiver. – Amrit Apr 30 '14 at 12:38
  • help me http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya Aug 06 '15 at 05:36
  • how to attach multiple files ..? – Moinkhan Jul 28 '16 at 10:08
  • 2
    @Amritpal that is exactly what happens when the file you passed in the intent is private to your application. Try copying the file to a public location on the device before and share it with that new url (ex. copy it to the External Storage folder first). – gjsalot Dec 14 '16 at 15:54
  • @EktaDushanj : What's the issue you're facing? – Mehul Ranpara Aug 11 '17 at 12:13
10
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("application/image");

Uri uri = Uri.parse("file://" + filepath);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(emailIntent);
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
yteng
  • 109
  • 1
  • 2
  • can someone please help me how to use this code to send image from my assets folder – Erum Feb 24 '14 at 18:47
  • 1
    You may not even need the "setType" because that would conflict with your having done 'setType("text/html"), right? It seems the Intent.EXTRA_STREAM figures out the type for the attachment; at least it worked for me when I removed the second "setType". And the mail arrived at the destination MTA with Content-Type: image/jpeg; name="photo000001.jpg" on the message part for the image. – idarwin Jun 17 '14 at 02:46
  • can you explain how can I get this filepath ? – Abdulsamet Kılınçarslan Jul 08 '21 at 14:18
0
//add below in Manifest file  
 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>

// create xml folder inside file name file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  -->
    <external-path
        name="my_images"
        path="data/data/com.cw.getmycolor/cache" />
    <external-path
        name="my_images"
        path="Android/data/com.cw.getmycolor/files/Pictures" />

    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
    <external-path
        name="share"
        path="/" />

</paths>

// send method email

    private void shareApp() {
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("application/image");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"demo@gmail.com");

    emailIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", new 

    File(uri.getPath())));/*getImageContentUri(this,new File(uri.getPath())));*/
            emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), 201);

        }
adarsh
  • 403
  • 3
  • 8