如何使用Intent.EXTRA_TEXT发送SQLite数据库的电子邮件?

时间:2018-03-05 23:34:51

标签: java android sqlite android-intent cursor

我想简单地发送一封电子邮件,其中包含我的数据库中的数据作为电子邮件中的文本。

我搜索了很多帖子,并且不想作为附件发送,我不想保存到外部存储等,也无法找到我要找的内容。

我希望填充数据库中的所有行,但只能设法获得单行。任何帮助将不胜感激。

    @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[]{"recipent@gmail.com"});
                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"));

1 个答案:

答案 0 :(得分:0)

感谢CommonsWare回答这个问题。我确实使用了Stringbuilder并且它完美无缺。正是我想要的。

现在是代码。

     @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[]{"recipent@gmail.com"});
                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"));