Search code examples
androidandroid-studiogmail

Gmail Error : 'cyk: Failed to create local attachment' when adding image attachment


im my app people can select up to 6 images, at the end of the app i want to add these images to an email to the appropriate email adress.

the system works but when i added :

i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

gmail gives this error: 'cyk: Failed to create local attachment'

after some testing i found out this is caused by not all images always being there, if i add all images it only sends the last one.

here is the code:

 public void SendButtonDown(View v){
    Intent intent = getIntent();

    Uri MAIL_IMAGES_URI = Uri.parse("file://"+MAIL_IMAGES);
    Uri MAIL_IMAGES_URI2 = Uri.parse("file://"+MAIL_IMAGES2);
    Uri MAIL_IMAGES_URI3 = Uri.parse("file://"+MAIL_IMAGES3);
    Uri MAIL_IMAGES_URI4 = Uri.parse("file://"+MAIL_IMAGES4);
    Uri MAIL_IMAGES_URI5 = Uri.parse("file://"+MAIL_IMAGES5);
    Uri MAIL_IMAGES_URI6 = Uri.parse("file://"+MAIL_IMAGES6);

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL   , new String[]{"[email protected]"});
    i.putExtra(Intent.EXTRA_SUBJECT , "Subject");
    i.putExtra(Intent.EXTRA_TEXT    , textVar);
    i.setType("application/image");


    i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    i.putExtra(Intent.EXTRA_STREAM  , MAIL_IMAGES_URI);
    i.putExtra(Intent.EXTRA_STREAM  , MAIL_IMAGES_URI2);
    i.putExtra(Intent.EXTRA_STREAM  , MAIL_IMAGES_URI3);
    i.putExtra(Intent.EXTRA_STREAM  , MAIL_IMAGES_URI4);
    i.putExtra(Intent.EXTRA_STREAM  , MAIL_IMAGES_URI5);
    i.putExtra(Intent.EXTRA_STREAM  , MAIL_IMAGES_URI6);


    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex){
        Toast.makeText(AanbiedenStap3.this, "no email installed", 
        Toast.LENGTH_SHORT).show();
    }
    finish();
}
}

Here is the Full Logcat:

09-23 15:16:44.065 11793-11793/? E/Gmail: Error adding attachment
                                      cyk: Failed to create local attachment
                                          at cyl.a(SourceFile:344)
                                          at cfe.a(SourceFile:3353)
                                          at cfn.run(SourceFile:6191)
                                          at cfe.a(SourceFile:19310)
                                          at cfe.a(SourceFile:1198)
                                          at cfe.C(SourceFile:1307)
                                          at cfe.onCreate(SourceFile:8505)
                                          at com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:201)
                                          at android.app.Activity.performCreate(Activity.java:6221)
                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
                                          at android.app.ActivityThread.access$900(ActivityThread.java:172)
                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                          at android.os.Looper.loop(Looper.java:145)
                                          at android.app.ActivityThread.main(ActivityThread.java:5832)
                                          at java.lang.reflect.Method.invoke(Native Method)
                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Thanks in advance :)


Solution

  • I have fixed the app, in case you also ran into this problem here is the fix:

    public void SendButtonDown(View v){
        Intent intent = getIntent();
    
        List<String> filePaths = new ArrayList<>();
        if(MAIL_IMAGES != null) {
            filePaths.add(MAIL_IMAGES);
        }
        if(MAIL_IMAGES2 != null) {
            filePaths.add(MAIL_IMAGES2);
        }
        if(MAIL_IMAGES3 != null) {
            filePaths.add(MAIL_IMAGES3);
        }
        if(MAIL_IMAGES4 != null) {
            filePaths.add(MAIL_IMAGES4);
        }
        if(MAIL_IMAGES5 != null) {
            filePaths.add(MAIL_IMAGES5);
        }
        if(MAIL_IMAGES6 != null) {
            filePaths.add(MAIL_IMAGES6);
        }
    
        final Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_EMAIL   , new String[]{"[email protected]"});
        i.putExtra(Intent.EXTRA_SUBJECT , "subject");
        i.putExtra(Intent.EXTRA_TEXT    , data);
    
        ArrayList<Uri> uris = new ArrayList<>();
    
        for (String file : filePaths){
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }
        i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        i.putExtra(Intent.EXTRA_STREAM, uris);
        startActivity(Intent.createChooser(i, "Send mail..."));
    
        finish();
    }
    }