2

In my app, i have a customize dialog from where i am getting the user input as textedit, sppiner, datepicker. I also have a "send" button there in the dialog. I want that after filling the fields in the dialog as soon the user presses on send button the data he/she might have filled there should get bundled as a .csv and get attached to the email and get sent through an email directly without opening the default email screen.

Any help will highly be appreciated.

mrana

Luis
  • 3,451
  • 1
  • 27
  • 41
mrana
  • 67
  • 2
  • 12

1 Answers1

3

Get the data from your EditText, Spinner, and DatePicker and store it in a String using the desired delimiter for your csv (e.g., comma, semicolon, tab, space, etc.).

Next save the file and then use Intent.Action_SEND along with Intent.CreateChooser to send the file as an attachment. If your file is stored internally (i.e., it's private) then you also need to use a ContentProvider (see this link).

Here's an example:

//For simplicity's sake let's say you have three methods 
//to get the value of your EditText, Spinner, 
//and DatePicker and these methods return a String

String editTextValue = getEditTextValue();
String spinnerTextValue = getSpinnerTextValue();
String datePickerTextValue = getDPTextValue();

//Create a String in csv format with the String values obtained 
//from the above fictitious methods. The delimiter in this case is the semicolon ";"

String myFileContentString = editTextValue + ";" + 
spinnerTextValue + ";" + 
datePickerTextValue + "\n";

//Save file to internal storage

FileOutputStream fos = openFileOutput("myfilename.csv", Context.MODE_WORLD_WRITEABLE);
fos.write(myFileContentString.getBytes());
fos.close();

//Send the file as an attachment

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "A CSV File");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "See attachment...");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, 
Uri.parse("content://" + MyContentProviderClass.AUTHORITY + "/" + "myfilename.csv"));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Don't forget to catch exceptions with try/catch

You will need to subclass a ContentProvider class and override the openFile method. See the links here and here for how to implement your own content provider.

In your ContentProvider subclass, you will want something like the following in the openFile method:

String fileLocation = getContext().getFilesDir() + File.separator + "myfilename.csv";

ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(fileLocation),
ParcelFileDescriptor.MODE_READ_ONLY);

return pfd;

And don't forget to update your AndroidManifest.xml with:

<provider android:name="my.package.content.provider.Class" 
android:authorities="my.package.content.provider"></provider></application>

The provider declaration in the manifest file goes within the application declaration.

Community
  • 1
  • 1
Luis
  • 3,451
  • 1
  • 27
  • 41
  • Are you storing the file in internal cache (as per you link)? If yes, then how to get it cleared automatically after the mail has been sent? – mrana Mar 14 '12 at 11:28
  • The example in the link stores the file in internal cache. My example stores the file in internal storage. Because I don't know when the email is sent, I delete the file in the onDestroy() mehtod of the activity by calling deleteFile("myfilename.csv"); – Luis Mar 14 '12 at 19:33
  • I have started working on this and need to know one more thing and that is how to add one more row to the same csv file? can i perform the same with \n ? – mrana Mar 15 '12 at 09:29
  • Yes, that's why my example above includes "\n". Just add the newline character "\n" at the end of your row before you start your next iteration for inserting the next row of data. – Luis Mar 16 '12 at 03:51