java - 添加多个图像作为电子邮件附件
问题描述
我有我正在测试的这段代码,它的目的是使用相机拍摄一些照片,然后将其传递给电子邮件应用程序,我面临的问题是只有第二张照片被附加,我知道原因是我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>
路径.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name="shared_images"
path="images/" />
</paths>
活动主.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_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/shareimage"
android:layout_width="200dp"
android:layout_height="200dp"/>
<Button
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:onClick="dispatchTakePictureIntent"
android:text="Button" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
解决方案
最后我找到了一个解决方案,这就是 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<Uri> uris = new ArrayList<>();
在变量下添加了
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);
在具有电子邮件意图的方法中,它可以工作!
推荐阅读
- css - dash_daq BooleanSwitch 因 Dash Bootstrap 组件的边框而失败
- list - 创建列表
来自列表 - git - 简单的 git bug 修复过程
- python - 通过 super() 覆盖构造函数,提供更少的父构造函数所需的参数
- parsing - 如何跨模型实例定义碰撞过滤器组?
- multithreading - 默认情况下为什么没有设置线程化 perl(带有 usethreads)?
- r - 如何在 R Markdown 中修复此错误代码?
- c - 打印函数计算链表中节点数的问题
- linux - mkdir: 无法创建目录 '': 没有这样的文件或目录
- java - Opencsv: Allow for a more customized error message when using @PreAssignmentValidator