当前位置 : 主页 > 手机开发 > android >

使用Gmail通过Implicit Intent发送邮件,并将图片作为Android中的附件?

来源:互联网 收集:自由互联 发布时间:2021-06-11
它的简单但在我的应用程序中创建问题.我正在使用Implicit Intent发送电子邮件,将IMAGE附加为附件.但是使用以下代码创建问题. File file = new File(path);Intent emailIntent = new Intent(Intent.ACTION_SEND
它的简单但在我的应用程序中创建问题.我正在使用Implicit Intent发送电子邮件,将IMAGE附加为附件.但是使用以下代码创建问题.

File file = new File(path);
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
emailIntent.setType("vnd.android.cursor.dir/email");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{""}); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Wedding App"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Sharing image via email."); 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.toString()));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

创建崩溃:

02-22 12:20:47.561: E/ActivityThread(6317): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@417c1fc8 that was originally registered here. Are you missing a call to unregisterReceiver()?
02-22 12:20:47.561: E/ActivityThread(6317): android.app.IntentReceiverLeaked: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@417c1fc8 that was originally registered here. Are you missing a call to unregisterReceiver()?
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1066)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1053)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1047)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341)
02-22 12:20:47.561: E/ActivityThread(6317):     at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:65)
02-22 12:20:47.561: E/ActivityThread(6317):     at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:99)
02-22 12:20:47.561: E/ActivityThread(6317):     at com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:53)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.Activity.performCreate(Activity.java:4470)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.os.Looper.loop(Looper.java:137)
02-22 12:20:47.561: E/ActivityThread(6317):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-22 12:20:47.561: E/ActivityThread(6317):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 12:20:47.561: E/ActivityThread(6317):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 12:20:47.561: E/ActivityThread(6317):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
02-22 12:20:47.561: E/ActivityThread(6317):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-22 12:20:47.561: E/ActivityThread(6317):     at dalvik.system.NativeStart.main(Native Method)
试试下面的代码.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("png/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
                "mail--id" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
Uri uri = Uri.fromFile(new File(Environment
                .getExternalStorageDirectory(), "/saved_images/MyImage.png"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
emailIntent.setType("text/plain");
startActivity(emailIntent);

并且不要忘记在清单文件中添加以下权限.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
网友评论