Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 发送带附件的电子邮件时出错大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个问题,我想发送带有图片附件的电子邮件,但从弹出菜单中选择Gmail选项时,它总是会返回错误.我没有找到同样背后的原因.请建议我正确的解决方案.

码:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBjeCT,"Halosys GreeTings");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"");
emailIntent.putExtra(Intent.EXTRA_STREAM,bm);
emailIntent.setType("image/png");
startActivity(Intent.createChooser(emailIntent,"Send mail..."));

错误堆栈:

12-28 11:42:37.456: ERROR/AndroidRuntime(21930): java.lang.RuntimeException: Unable to start activity ComponenTinfo{Com.google.android.gm/com.google.android.gm.ComposeActivity}: java.lang.NullPointerException
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.ActivityThread$H.handlemessage(ActivityThread.java:2033)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.os.Handler.dispatchmessage(Handler.java:99)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.os.Looper.loop(Looper.java:123)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at java.lang.reflect.Method.invokeNative(Native Method)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at java.lang.reflect.Method.invoke(Method.java:521)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at dalvik.system.NativeStart.main(Native Method)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930): Caused by: java.lang.NullPointerException
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.google.android.gm.provider.Gmail$AttachmentOrigin.localFileExtras(Gmail.java:2194)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.google.android.gm.ComposeArea.addAttachment(ComposeArea.java:732)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.google.android.gm.ComposeArea.initFromExtras(ComposeArea.java:685)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.google.android.gm.ComposeActivity.initFromExtras(ComposeActivity.java:1482)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.google.android.gm.ComposeActivity.finishOnCreateAfterAccountSELEcted(ComposeActivity.java:1021)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at com.google.android.gm.ComposeActivity.onCreate(ComposeActivity.java:259)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.instrumentation.callActivityOnCreate(instrumentation.java:1047)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):     ... 11 more

提前致谢.

解决方法

不知道你如何发送它,但这段代码工作正常.

public void doSendFile() {
    String filename = "myFilename.txt";

    String externalStorageDirectory = Environment
        .getExternalStorageDirectory().toString();
    String myDir = externalStorageDirectory + "/myDir/";  // the file will be in myDir
    Uri uri = Uri.parse("file://" + myDir + fileName);
    Intent i = new Intent(Intent.ACTION_SEND);
    try {
        myFileHandle.close(); // you may want to be sure that the file is closed correctly
    } catch (IOException E) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    i.setType("text/plain"); // as you can see I am sending a simple txt file here
    i.putExtra(Intent.EXTRA_EMAIL,new String[] { "sendTo@gmail.com" });
    i.putExtra(Intent.EXTRA_SUBjeCT,"the subject text");
    i.putExtra(Intent.EXTRA_TEXT,"extra text body");
    Log.i(getClass().getSimplename(),"logFile=" + uri);
    i.putExtra(Intent.EXTRA_STREAM,uri);

    try {
        startActivity(Intent.createChooser(i,"Send mail..."));
    } catch (android.content.ActivityNotFoundException eX) {
        Toast.makeText(getBaseContext(),"There are no email clients installed.",Toast.LENGTH_SHORT)
                .show();
    }
}

也确定你有

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在您的清单中,您可以在外部存储中实际创建该文件.

附:我从未设法直接从内部存储发送文件.

大佬总结

以上是大佬教程为你收集整理的android – 发送带附件的电子邮件时出错全部内容,希望文章能够帮你解决android – 发送带附件的电子邮件时出错所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。