通过意图将多个附件(图像和PDF)发送到gmail
Send multiple attachments( images and PDFs) to gmail through intent
我有一个文件选择器,它为我提供所选文件的 URI(可以是多个或单个),现在我想使用 intent 将这些所选文件(图像和 pdf)发送到 gmail 应用程序。我尝试了所有可用的在线方式来发送附件,但都没有成功。请指导我解决这个问题,卡在这里好几天了。
使用 EasyPermissions 请求运行时权限:
if (!EasyPermissions.hasPermissions(AddEventActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
EasyPermissions.requestPermissions(AddEventActivity.this, "Read External data",2, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
文件选择器:
Intent chooseFile = new Intent();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
chooseFile.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
else {
chooseFile.setAction(Intent.ACTION_GET_CONTENT);
}
chooseFile.setType("*/*");
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
将返回的 URI 存储在 HashMap 中:uriMap
Gmail 意图
Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);
emailSelectorIntent.setData(Uri.parse("mailto:"));
Log.d("mailList", Arrays.toString(subarray));
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, subarray);
emailIntent.putExtra(Intent.EXTRA_SUBJECT,(CharSequence) eventName.getText().toString());
ArrayList<String> bodyList = new ArrayList<>();
String body = eventDesc.getText().toString() + " at " + eventVenue.getText().toString() + ", " + hourSelect + ":" + minuteSelect;
bodyList.add(body);
emailIntent.putExtra(Intent.EXTRA_TEXT, bodyList);
emailIntent.setSelector(emailSelectorIntent);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
ArrayList<Uri> uris = new ArrayList<Uri>();
Iterator iterator = uriMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapElement = (Map.Entry) iterator.next();
uris.add((Uri) mapElement.getValue());
}
if (uris.size() != 0)
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Choose an email application..."));
OnActivityResult 代码
如果选择了多个文件
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();}
否则如果选择单个文件
Uri uri;
uri = data.getData();
LogCat错误
2020-08-01 16:56:16.414 30575-30575/? E/Gmail: ComposeActivity:Error adding attachment
gon: SecurityException when openAssetFileDescriptor.
at goo.a(PG:5)
at goo.a(PG:45)
at dvt.a(PG:137)
at duz.run(PG:2)
at dvt.a(PG:176)
at dvt.a(PG:144)
at dvt.a(PG:443)
at drq.a(Unknown Source:24)
at bbkz.a(Unknown Source:19)
at bdvu.a(Unknown Source:2)
at bdvw.run(PG:9)
at bdzb.run(Unknown Source:7)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at bbja.run(PG:2)
at aqsr.run(Unknown Source:18)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
感谢帮助。
提前致谢。
文件选择器代码:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
startActivityForResult(Intent.createChooser(intent, "ChooseFile"), PICKFILE_RESULT_CODE);
意图代码:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, subarray);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("vnd.android.cursor.dir/email");
ArrayList<Uri> uris=new ArrayList<Uri>();
if (uriMap.size()!=0) {
Iterator i= uriMap.entrySet().iterator();
while(i.hasNext()){
Map.Entry mapElement = (Map.Entry) i.next();
uris.add((Uri)mapElement.getValue());
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
}
intent.setPackage("com.google.android.gm");
startActivityForResult(intent, 101);
使用 intent.setType()
和 intent.setPackage()
对我有用!
我有一个文件选择器,它为我提供所选文件的 URI(可以是多个或单个),现在我想使用 intent 将这些所选文件(图像和 pdf)发送到 gmail 应用程序。我尝试了所有可用的在线方式来发送附件,但都没有成功。请指导我解决这个问题,卡在这里好几天了。
使用 EasyPermissions 请求运行时权限:
if (!EasyPermissions.hasPermissions(AddEventActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
EasyPermissions.requestPermissions(AddEventActivity.this, "Read External data",2, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
文件选择器:
Intent chooseFile = new Intent();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
chooseFile.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
else {
chooseFile.setAction(Intent.ACTION_GET_CONTENT);
}
chooseFile.setType("*/*");
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
将返回的 URI 存储在 HashMap 中:uriMap
Gmail 意图
Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);
emailSelectorIntent.setData(Uri.parse("mailto:"));
Log.d("mailList", Arrays.toString(subarray));
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, subarray);
emailIntent.putExtra(Intent.EXTRA_SUBJECT,(CharSequence) eventName.getText().toString());
ArrayList<String> bodyList = new ArrayList<>();
String body = eventDesc.getText().toString() + " at " + eventVenue.getText().toString() + ", " + hourSelect + ":" + minuteSelect;
bodyList.add(body);
emailIntent.putExtra(Intent.EXTRA_TEXT, bodyList);
emailIntent.setSelector(emailSelectorIntent);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
ArrayList<Uri> uris = new ArrayList<Uri>();
Iterator iterator = uriMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapElement = (Map.Entry) iterator.next();
uris.add((Uri) mapElement.getValue());
}
if (uris.size() != 0)
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Choose an email application..."));
OnActivityResult 代码
如果选择了多个文件
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();}
否则如果选择单个文件
Uri uri;
uri = data.getData();
LogCat错误
2020-08-01 16:56:16.414 30575-30575/? E/Gmail: ComposeActivity:Error adding attachment
gon: SecurityException when openAssetFileDescriptor.
at goo.a(PG:5)
at goo.a(PG:45)
at dvt.a(PG:137)
at duz.run(PG:2)
at dvt.a(PG:176)
at dvt.a(PG:144)
at dvt.a(PG:443)
at drq.a(Unknown Source:24)
at bbkz.a(Unknown Source:19)
at bdvu.a(Unknown Source:2)
at bdvw.run(PG:9)
at bdzb.run(Unknown Source:7)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at bbja.run(PG:2)
at aqsr.run(Unknown Source:18)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
感谢帮助。
提前致谢。
文件选择器代码:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
startActivityForResult(Intent.createChooser(intent, "ChooseFile"), PICKFILE_RESULT_CODE);
意图代码:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, subarray);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("vnd.android.cursor.dir/email");
ArrayList<Uri> uris=new ArrayList<Uri>();
if (uriMap.size()!=0) {
Iterator i= uriMap.entrySet().iterator();
while(i.hasNext()){
Map.Entry mapElement = (Map.Entry) i.next();
uris.add((Uri)mapElement.getValue());
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
}
intent.setPackage("com.google.android.gm");
startActivityForResult(intent, 101);
使用 intent.setType()
和 intent.setPackage()
对我有用!