// create a csv file of the different locations, and export it as an attachment to an email public void exportList(View v) { // Construct the csv file from the database GameDBHelper mDBHelper = new GameDBHelper(getBaseContext()); SQLiteDatabase db = mDBHelper.getReadableDatabase(); // *********************************************************************************** // These values will be used for a CursorAdapter technique for populating the ListView // *********************************************************************************** String[] projection = { BaseColumns._ID, ContractCoordinates.CoordinatesEntry.COLUMN_NAME_ID, ContractCoordinates.CoordinatesEntry.COLUMN_NAME_LOCATION_NAME, ContractCoordinates.CoordinatesEntry.COLUMN_NAME_DESCRIPTION, ContractCoordinates.CoordinatesEntry.COLUMN_NAME_LATITUDE, ContractCoordinates.CoordinatesEntry.COLUMN_NAME_LONGITUDE, ContractCoordinates.CoordinatesEntry.COLUMN_NAME_DATE }; String sortOrder = BaseColumns._ID + " DESC"; Cursor c2 = db.query( ContractCoordinates.CoordinatesEntry.TABLE_NAME, projection, null, null, null, null, sortOrder); c2.moveToFirst(); String dataString = ""; // = "\"" + "test location" +"\",\"" + "test lat" + "\",\"" + "test long" + "\""; for (int i = 0; i < c2.getCount(); i++) { dataString = dataString + "\"" + c2.getString( c.getColumnIndex(ContractCoordinates.CoordinatesEntry.COLUMN_NAME_LOCATION_NAME)) + "\",\"" + c2.getString( c.getColumnIndex(ContractCoordinates.CoordinatesEntry.COLUMN_NAME_LATITUDE)) + "\",\"" + c2.getString( c.getColumnIndex(ContractCoordinates.CoordinatesEntry.COLUMN_NAME_LONGITUDE)) + "\"" + "\n"; c2.moveToNext(); } db.close(); String columnString = "\"Place\",\"Latitude\",\"Longitude\""; String combinedString = columnString + "\n" + dataString; File file = null; File root = Environment.getExternalStorageDirectory(); if (root.canWrite()) { File dir = new File(root.getAbsolutePath() + "/PersonData"); dir.mkdirs(); file = new File(dir, "Data.csv"); FileOutputStream out = null; try { out = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { out.write(combinedString.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } Uri u1 = null; u1 = Uri.fromFile(file); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "LatLong List"); sendIntent.putExtra(Intent.EXTRA_STREAM, u1); sendIntent.setType("text/html"); startActivity(sendIntent); }