Search code examples
javaandroidsqliteandroid-intentcursor

How to send email of SQLite database using Intent.EXTRA_TEXT?


I am trying to simply send an email with the data from my database as text in the email.

I have searched a ton of posts, and do not want to send as an attachment, I do not want to save to external storage etc. and cannot find what I am looking for.

I would like all the rows from the database to populate, but can only manage to get a single row. Any help would be greatly appreciated.

    @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                String[] projection = {
                        LogEntry._ID,
                        LogEntry.COLUMN_LOG_DATE,
                        LogEntry.COLUMN_LOG_DESTINATION,
                        LogEntry.COLUMN_LOG_PURPOSE,
                        LogEntry.COLUMN_LOG_MILEAGE};


                Cursor cursor = getContentResolver().query(
                        LogEntry.CONTENT_URI,
                        projection,
                        null,
                        null,
                        null);

                //TODO ???????????? I want the cursor to continue and get all the data from the database???
                //This is the current result in the email.
                // 1
                // 05 Mar 2018
                // 12:10:52 PM
                // 0
                // 0
                // 88031


                // Extract the properties from cursor
                // Find columns of log attributes that I am interested in
                int idColumnIndex = cursor.getColumnIndex(LogEntry._ID);
                int dateColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_DATE);
                int destinationColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_DESTINATION);
                int purposeColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_PURPOSE);
                int mileageColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_MILEAGE);

                String id = cursor.getString(idColumnIndex);
                String date = cursor.getString(dateColumnIndex);
                int destination = cursor.getInt(destinationColumnIndex);
                int purpose = cursor.getInt(purposeColumnIndex);
                String mileage = cursor.getString(mileageColumnIndex);


                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("message/rfc822");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "All Logs for " + LogEntry.COLUMN_LOG_DATE);
                emailIntent.putExtra(Intent.EXTRA_TEXT, id + "\n" + date + "\n" + destination + "\n" + purpose + "\n" + mileage);
                startActivity(Intent.createChooser(emailIntent, "Select App"));

Solution

  • Thank You CommonsWare for answering this question. I indeed used the Stringbuilder and it worked flawlessly. Exactly what I wanted.

    Here is the code now.

         @Override
                public void onClick(DialogInterface dialogInterface, int i) {
    
                    StringBuilder stringBuilder = new StringBuilder();
    
                    String[] projection = {
                            LogEntry._ID,
                            LogEntry.COLUMN_LOG_DATE,
                            LogEntry.COLUMN_LOG_DESTINATION,
                            LogEntry.COLUMN_LOG_PURPOSE,
                            LogEntry.COLUMN_LOG_MILEAGE};
    
    
                    Cursor cursor = getContentResolver().query(
                            LogEntry.CONTENT_URI,
                            projection,
                            null,
                            null,
                            null);
    
                    //if the cursor isn't null we will essentially iterate over rows and then columns
                    //to form a table of data as per database.
                    if (cursor != null) {
    
                        //more to the first row
                        cursor.moveToFirst();
    
                        //iterate over rows
                        for (int index = 0; index < cursor.getCount(); index++) {
    
                            //iterate over the columns
                            for(int j = 0; j < cursor.getColumnNames().length; j++){
    
                                //append the column value to the string builder and delimit by a pipe symbol
                                stringBuilder.append(cursor.getString(j) + " | ");
                            }
                            //add a new line carriage return
                            stringBuilder.append("\n");
    
                            //move to the next row
                            cursor.moveToNext();
                        }
                        //close the cursor
                        cursor.close();
                    }
    
    
    
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.setType("message/rfc822");
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Logs for " + ;
                    emailIntent.putExtra(Intent.EXTRA_TEXT, stringBuilder.toString()); //id + "\n" + date + "\n" + destination + "\n" + purpose + "\n" + mileage);
                    startActivity(Intent.createChooser(emailIntent, "Select App"));