How to link data received from custom dialog (edittext, datepicker, spinner etc) in .csv and connect to email in android?

In my application, I have a settings dialog, where I get user input as textedit, sppiner, datepicker. I also have a submit button in the dialog box. I want that after filling in the fields in the dialog box, as soon as the user clicks the submit button, the data that he may have filled in should be bundled in the form of .csv and attached to the email and sent via email directly, without opening the by default.

Any help would be greatly appreciated.

mrana

+2
source share
1 answer

Get the data from your EditText, Spinner and DatePicker and save it in a line using the necessary separator for your csv (e.g. comma, semicolon, tab, space, etc.).

Then 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 (that is, it is closed), you also need to use a ContentProvider (see Link).

Here is an example:

//For simplicity sake let 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...")); 

Remember to catch exceptions with try / catch

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

In a subclass of ContentProvider in the openFile method, you will need the following:

 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 vendor declaration in the manifest file is included in the application declaration.

+3
source

All Articles