在电子邮件中发送多个附件:android.intent.extra.STREAM期望Parcelable

时间:2011-04-19 20:32:26

标签: android email android-intent attachment

  

可能重复:
  how to attach multiple files to email client in android.

您好,

我无法发送包含多个附件的电子邮件。

以下是我在网站和教程中找到的代码:

String[] mArray = getResources().getStringArray(R.array.pdf_entries);               
int i = 0;
Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
theIntent.setType("application/pdf");

ArrayList<Uri> uris = new ArrayList<Uri>();

for (String file : mArray) {
    if (booleans[i]) {
        File fileIn = new File("/sdcard/mydir/"+file+".pdf");
        Uri u = Uri.fromFile(fileIn);
        Log.i("","** add: "+u);
        uris.add(u);
    }
    i++;
}
theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(theIntent,"Select email application."));

但我总是得到这个错误:

04-19 20:25:03.340: WARN/Bundle(5826): Key android.intent.extra.STREAM expected Parcelable but value was a java.util.ArrayList.  The default value <null> was returned.
04-19 20:25:03.356: WARN/Bundle(5826): Attempt to cast generated internal exception:
04-19 20:25:03.356: WARN/Bundle(5826): java.lang.ClassCastException: java.util.ArrayList
04-19 20:25:03.356: WARN/Bundle(5826):     at android.os.Bundle.getParcelable(Bundle.java:1106)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.content.Intent.getParcelableExtra(Intent.java:3396)
04-19 20:25:03.356: WARN/Bundle(5826):     at com.android.email.activity.MessageCompose.initFromIntent(MessageCompose.java:1306)
04-19 20:25:03.356: WARN/Bundle(5826):     at com.android.email.activity.MessageCompose.onCreate(MessageCompose.java:334)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.os.Looper.loop(Looper.java:123)
04-19 20:25:03.356: WARN/Bundle(5826):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-19 20:25:03.356: WARN/Bundle(5826):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 20:25:03.356: WARN/Bundle(5826):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 20:25:03.356: WARN/Bundle(5826):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-19 20:25:03.356: WARN/Bundle(5826):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-19 20:25:03.356: WARN/Bundle(5826):     at dalvik.system.NativeStart.main(Native Method)
04-19 20:25:03.403: INFO/SurfaceFlinger(1305): 

非常感谢任何建议/教程。

2 个答案:

答案 0 :(得分:9)

how to attach multiple files to email client in android

编辑(指向差异点):

Intent.ACTION_SEND!= Intent.ACTION_SEND_MULTIPLE

答案 1 :(得分:8)

    public class EmailActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);       
    }

    public void buttonClickMethod(View buttonObject){
            emailMultipleFiles(arrayOfFilestoSend[],"Subject","Message Content");
    }

    private void emailMultipleFiles(File[] attachmentFiles, String subjectContent, String messageContent) {
            Intent email = new Intent(Intent.ACTION_SEND_MULTIPLE);
            email.putExtra(Intent.EXTRA_SUBJECT, subjectContent);
            email.putExtra(Intent.EXTRA_TEXT, messageContent);

            ArrayList<Uri> uriList = new ArrayList<Uri>();
            for (int i = 0; i < attachmentFiles.length; i++) {
                uriList.add(Uri.fromFile(attachmentFiles[i]));
            }

            email.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
            email.setType("text/plain");
            /** or use **/ 
            email.setType("message/rfc822");
            startActivity(email);
    }
    }