How to bundle the data captured from customized dialog(edittext,datepicker,spinner e.t.c) in .csv and attach to email in android?

懵懂的女人 提交于 2020-01-04 05:40:09

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/9645211/how-to-bundle-the-data-captured-from-customized-dialogedittext-datepicker-spinn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!