Attach XLS file (Excel) to Email

My application uses the intent method to send email to users as a convenient way to export Excel spreadsheet data (created by the JExcell API ).

The file is contained on the SD card in a folder named records .

The file I'm trying to send is a call to measurments.xls.

I tested the file presence in the code before sending. The email composer shows the attachment, but when I send and then receive the email, the attachment is not there.

However, if I substitute the excel file for the png image, the attachment will be received. So what gives?

Below is the code that I use to send emails, it is just a static method with class partitioning in my own way.

    public static  void sendEmailWithAttachment(Context ctx, String to,String subject, String message, String fileAndLocation)
    {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {to}); 

        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,  subject); 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,  message); 


          File file = new File(fileAndLocation);
         //  File file = getFileStreamPath();
           if (file.exists())
           {
               Log.v("Farmgraze", "Email file_exists!" );
           }
           else
           {
               Log.v("Farmgraze", "Email file does not exist!" );
           }


        Log.v("FarmGraze", "SEND EMAIL FileUri=" + Uri.parse("file:/"+ fileAndLocation));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+  fileAndLocation));

        ctx.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }//end method

So what do I need to do to get the xls file? Change the mime types in the second line of the method code? If so, then what. Any helpful tips would be greatly appreciated.

Thanks for reading.

A.
+3
source share
2 answers

Ok, people just added a closure to this question. I have found a solution.

The problem was that the path to the String file sent to the URI must have three slashes.

How in:

file:///sdcard/somefolder/some_file.xls.

Also for excel documents, you must set the type as follows:

emailIntent.setType("application/excel");

, . - , , , , .

mime -, mime .

, .

+6

, mime. :

MimeTypeMap mime = MimeTypeMap.getSingleton();     
String mimeTypeForXLSFile = mime.getMimeTypeFromExtension(".xls");

emailIntent.setType(mimeTypeForXLSFile);
0

All Articles