添加多个图像作为电子邮件附件

Posted

技术标签:

【中文标题】添加多个图像作为电子邮件附件【英文标题】:Adding multiple images as an email attachment 【发布时间】:2021-10-30 06:28:52 【问题描述】:

我有我正在测试的这段代码,它的目的是使用相机拍摄一些照片,然后将其传递给电子邮件应用程序,我面临的问题是只有第二张照片被附加,我知道原因是我像这样使用intent.putExtra(Intent.EXTRA_STREAM, uri); 两次 intent.putExtra(Intent.EXTRA_STREAM, uri);intent.putExtra(Intent.EXTRA_STREAM, uri2); 但我想不出任何其他方法,我尝试了ACTION_SEND_MULTIPLE 它只是打开没有附件的邮件应用程序。

以下是完整代码: MainActivity.java:

package com.example.myapplicationtest6;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity 

    Button share, pic;
    ImageView imageView, imageView2;
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        share = findViewById(R.id.share);
        imageView = findViewById(R.id.shareimage);
        imageView2 = findViewById(R.id.imageView2);
        pic = findViewById(R.id.pic);

        // initialising text field where we will enter data
        share.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                // Now share image function will be called
                // here we  will be passing the text to share
                // Getting drawable value from image
                BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
                Bitmap bitmap1 = bitmapDrawable.getBitmap();

                BitmapDrawable bitmapDrawable2 = (BitmapDrawable) imageView2.getDrawable();
                Bitmap bitmap2 = bitmapDrawable2.getBitmap();
                shareImageandText(bitmap1, bitmap2);


            
        );
    
    public void dispatchTakePictureIntent(View view) 
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try 
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
         catch (ActivityNotFoundException e) 
            // display error state to the user
        
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        if (imageView.getDrawable() == null && requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap);
         else if (imageView2.getDrawable() == null && requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView2.setImageBitmap(imageBitmap);
        
    

    private void shareImageandText(Bitmap bitmap, Bitmap bitmap2) 
        Uri uri = getmageToShare(bitmap);
        Uri uri2 =getmageToShare2(bitmap2);

        Intent intent = new Intent(Intent.ACTION_SEND);

        // putting uri of image to be shared
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(Intent.EXTRA_STREAM, uri2);

        // adding text to share
        intent.putExtra(Intent.EXTRA_TEXT, "Sharing Image");

        // Add subject Here
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");

        // setting type to image
        intent.setType("image/png");

        // calling startactivity() to share
        startActivity(Intent.createChooser(intent, "Share Via"));
    

    // Retrieving the url to share
    private Uri getmageToShare(Bitmap bitmap) 
        File imagefolder = new File(getCacheDir(), "images");
        Uri uri = null;
        try 
            imagefolder.mkdirs();
            File file = new File(imagefolder, "shared_image.png");
            FileOutputStream outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);

            outputStream.flush();
            outputStream.close();
            uri = FileProvider.getUriForFile(this, "com.example.myapplicationtest6.fileprovider", file);
         catch (Exception e) 
            Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
        
        return uri;
    
    private Uri getmageToShare2(Bitmap bitmap) 
        File imagefolder = new File(getCacheDir(), "images");
        Uri uri = null;
        try 
            imagefolder.mkdirs();
            File file = new File(imagefolder, "shared_image2.png");
            FileOutputStream outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
            outputStream.flush();
            outputStream.close();
            uri = FileProvider.getUriForFile(this, "com.example.myapplicationtest6.fileprovider", file);
         catch (Exception e) 
            Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
        
        return uri;
    

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplicationtest6">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application


        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplicationTest6">
        <activity

            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.myapplicationtest6.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths" />
        </provider>
    </application>

</manifest>

paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path
        name="shared_images"
        path="images/" />
</paths>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    
    <ImageView
        android:id="@+id/shareimage"
        android:layout_
        android:layout_/>

    
    <Button
        android:id="@+id/share"
        android:layout_
        android:layout_
        android:layout_marginTop="10dp"
        android:background="#FFFFFF"
        android:padding="10dp"
        android:text="Click here to Share "
        android:textSize="10dp" />

    <Button
        android:id="@+id/pic"
        android:layout_
        android:layout_
        android:onClick="dispatchTakePictureIntent"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_
        android:layout_ />

</LinearLayout>

【问题讨论】:

也许可以压缩照片并使用单个附件。 你能给我分享一个指南链接吗? 将 uris 放入 Uri ArrayList 中,然后使用 putParcebleArrayListExtra() 或类似的东西。每个月都会问你的问题。搜索。 【参考方案1】:

终于找到了解决办法 这就是 MainActivity.java 的样子

package com.example.myapplicationtest6;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity 

    Button share, pic;
    ImageView imageView, imageView2;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    ArrayList<Uri> uris = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        share = findViewById(R.id.share);
        imageView = findViewById(R.id.shareimage);
        imageView2 = findViewById(R.id.imageView2);
        pic = findViewById(R.id.pic);

        // initialising text field where we will enter data
        share.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                // Now share image function will be called
                // here we  will be passing the text to share
                // Getting drawable value from image
                BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
                Bitmap bitmap1 = bitmapDrawable.getBitmap();

                BitmapDrawable bitmapDrawable2 = (BitmapDrawable) imageView2.getDrawable();
                Bitmap bitmap2 = bitmapDrawable2.getBitmap();
                shareImageandText(bitmap1, bitmap2);


            
        );
    
    public void dispatchTakePictureIntent(View view) 
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try 
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
         catch (ActivityNotFoundException e) 
            // display error state to the user
        
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        if (imageView.getDrawable() == null && requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap);
         else if (imageView2.getDrawable() == null && requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView2.setImageBitmap(imageBitmap);
        
    

    private void shareImageandText(Bitmap bitmap, Bitmap bitmap2) 
        Uri uri = getmageToShare(bitmap);
        Uri uri2 =getmageToShare2(bitmap2);

        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);

        uris.add(uri);
        uris.add(uri2);
        // putting uri of image to be shared

        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);


        // adding text to share
        intent.putExtra(Intent.EXTRA_TEXT, "Sharing Image");

        // Add subject Here
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");

        // setting type to image
        intent.setType("image/png");

        // calling startactivity() to share
        startActivity(Intent.createChooser(intent, "Share Via"));
    

    // Retrieving the url to share
    private Uri getmageToShare(Bitmap bitmap) 
        File imagefolder = new File(getCacheDir(), "images");
        Uri uri = null;
        try 
            imagefolder.mkdirs();
            File file = new File(imagefolder, "shared_image.png");
            FileOutputStream outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);

            outputStream.flush();
            outputStream.close();
            uri = FileProvider.getUriForFile(this, "com.example.myapplicationtest6.fileprovider", file);
         catch (Exception e) 
            Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
        
        return uri;
    
    private Uri getmageToShare2(Bitmap bitmap) 
        File imagefolder = new File(getCacheDir(), "images");
        Uri uri = null;
        try 
            imagefolder.mkdirs();
            File file = new File(imagefolder, "shared_image2.png");
            FileOutputStream outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
            outputStream.flush();
            outputStream.close();
            uri = FileProvider.getUriForFile(this, "com.example.myapplicationtest6.fileprovider", file);
         catch (Exception e) 
            Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
        
        return uri;
    

我在变量下添加了ArrayList&lt;Uri&gt; uris = new ArrayList&lt;&gt;();

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);

        uris.add(uri);
        uris.add(uri2);
        // putting uri of image to be shared

        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

在具有电子邮件意图的方法中,它可以工作!。

【讨论】:

太棒了!!太好了,你发现了。

以上是关于添加多个图像作为电子邮件附件的主要内容,如果未能解决你的问题,请参考以下文章

如何删除图像作为附件但显示在电子邮件正文中

使用 c# windows 应用程序在电子邮件正文中添加多个图像(内联)

如何将图像插入电子邮件消息正文而不是作为附件? [复制]

Laravel:如何将图像作为内容而不是附件嵌入电子邮件中

使用 Django 在 HTML 电子邮件模板中将图像作为内联附件发送

如何发送电子邮件附件?