Sending Email with attachment, sends a blank file.

Posted by pankaj on Stack Overflow See other posts from Stack Overflow or by pankaj
Published on 2011-02-24T07:19:47Z Indexed on 2011/02/24 7:24 UTC
Read the original article Hit count: 350

Filed under:
|

I am using this code:

File myDir = new File(getApplicationContext().getFilesDir().getAbsolutePath());
try {                
    Log.i("CSV Testing ", "CSV file creating");
    FileWriter fw = new FileWriter(myDir + "/myfile.csv");
    //
                 // write data to file
                 //   
    Log.i("CSV Testing ", "CSV file created and your data has been saved");
    // Process for sending email with CSV file
    File CSVFile = new File(myDir,"myfile.csv");
    if(CSVFile.exists()) {
       Log.i("CSV FILE", "CSV file exists");
    }  else {
       Log.i("CSV FILE", "CSV file not exists");
    }

    Log.i("SEND EMAIL TESTING", "Email sending");
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/csv");
    emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"myemailid"});            
    emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, "my subject");             
    emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, "_____________\n Regards \n Pankaj \n ____________ ");
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + CSVFile.getAbsolutePath()));
    emailIntent.setType("message/rfc822"); // Shows all application that supports SEND activity 
    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        showMSG("There are no email clients installed.");
    }

    Log.i("SEND EMAIL TESTING", "Email sent");

} catch (FileNotFoundException e) {
    Log.i("ExportCSV Exception", e.toString());
} catch (IOException e) {
    Log.i("ExportCSV Exception", e.toString());
}

But it sends myfile.csv as a blank file. I checked it from file explorer, where myfile.csv is not blank and contains right data. How can I solve this?

© Stack Overflow or respective owner

Related posts about java

Related posts about android