How to send Listview items to email as attachment, preferably as csv? Below is a sample of the Listview items, which I would like to send them by email as attachment:
public class MainActivity extends AppCompatActivity {
ListView listView;
// Define string array.
String[] listValue = new String[] {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue);
listView.setAdapter(adapter);
}
}
I have checked with some pertinent SO comments. this code is to export to a SDCard. What I wish is to directly send them without storing them to a SDCard. This comment shows how to send them as part of the message body. My intention is to send them as direct attachment. I would very much appreciate for helps in implementing this or for guiding me to some useful link or tutorial to achieve this.
EDIT (to add a code with which I am trying to access a data (saved Listview items as CSV) and attach them:
On buttonClick:
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String file = "/Data.CSV";
Uri uri = Uri.parse(directory+ file);
sendMail(getContext(), mailID, mailSubject, null, uri);
sendmail Function:
public void sendMail(Context context, String mailID, String subject, File attachment, Uri uri) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, mailID);
// Need to grant this permission
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Attachment
intent.setType("vnd.android.cursor.dir/email");
if (attachment != null)
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
else if (uri != null)
intent.putExtra(Intent.EXTRA_STREAM, uri);
}
This function is not working