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

问题描述 投票:0回答:1

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

我搜索了大量的帖子,并且不想作为附件发送,我不想保存到外部存储器等,也无法找到我要找的东西。

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

    @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"));
java android sqlite android-intent cursor
1个回答
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[]{"[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"));
© www.soinside.com 2019 - 2024. All rights reserved.