17

收件人正在接收电子邮件,但没有附件。这是代码,有高手知道我哪里出错了吗?

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

我得到:

file:// 附件路径必须指向 file://sdcard。忽略附件文件://...文件名是 MyApp.jpg

我没有得到图像,只有在短信上方。谢谢。

4

3 回答 3

40

试试下面的代码...

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..."));
于 2013-01-22T11:42:03.913 回答
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);
于 2013-05-13T22:25:36.007 回答
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);

        }
于 2020-02-28T06:10:47.960 回答