Kumar
Kumar

Reputation: 623

Android\Intent: Send an email with image attachment

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.

Upvotes: 17

Views: 45084

Answers (3)

adarsh
adarsh

Reputation: 531

//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,"[email protected]");

    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);

        }

Upvotes: 0

yteng
yteng

Reputation: 117

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);

Upvotes: 10

Mehul Ranpara
Mehul Ranpara

Reputation: 4255

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..."));

Upvotes: 40

Related Questions